pyslang Python Bindings for Slang SystemVerilog Compiler
pyslang provides Python bindings for the `slang` C++ library, a high-performance compiler for SystemVerilog. It enables Python applications to parse, elaborate, and perform advanced static analysis on SystemVerilog code. The current version is 10.0.0. `slang` has a regular release cadence, with major versions often introducing significant LRM compliance updates and performance improvements.
Common errors
-
ModuleNotFoundError: No module named 'pyslang'
cause You are attempting to import the library using its PyPI package name (`pyslang`) instead of its internal module name (`slang`).fixChange your import statement from `import pyslang` to `import slang`. -
SystemVerilog code that compiled in a previous pyslang version now produces errors or new warnings after upgrading.
cause `pyslang` has updated its compliance with the SystemVerilog LRM (Language Reference Manual), introducing stricter checks or new interpretations of the standard.fixReview the release notes for your `pyslang` version (and the underlying `slang` C++ library) to understand specific LRM changes. Modify your SystemVerilog source code to comply with the latest standard. -
AttributeError: 'Library' object has no attribute 'some_method_for_compiled_design'
cause You are trying to access methods or properties of the compiled design directly on the `Library` object. The `Library` object manages source files, while the `Compilation` object (returned by `getCompilation()`) holds the elaborated design.fixFirst, obtain the `Compilation` object using `compilation = library.getCompilation()`. Then, access methods related to the compiled design (e.g., getting diagnostics or the root symbol) on the `compilation` object. -
My SystemVerilog code has syntax errors, but my Python script completes without any visible output or exceptions.
cause You are not explicitly checking the diagnostics list returned by the compilation process. `pyslang` does not raise Python exceptions for SystemVerilog errors by default.fixAfter `compilation = library.getCompilation()`, you *must* check `diagnostics = compilation.getDiagnostics()` and iterate through the `diagnostics` list to print any errors or warnings found during compilation.
Warnings
- gotcha The PyPI package `pyslang` provides its functionality under the Python module name `slang`. Importing `pyslang` directly will result in a `ModuleNotFoundError`.
- breaking Major versions (e.g., v7.0, v9.0, v10.0) frequently introduce stricter compliance with the SystemVerilog Language Reference Manual (LRM). Code that previously compiled silently might now generate errors or warnings due to enhanced LRM enforcement or new static analysis checks.
- gotcha Compilation errors and warnings are not raised as Python exceptions but are returned as a list of `Diagnostic` objects via `compilation.getDiagnostics()`. Failing to check this list means you might unknowingly have an invalid design.
- breaking The underlying `slang` C++ library is actively developed, and while core APIs like `Library()` and `addSourceText()` are stable, more advanced features or introspection APIs might see changes between major `pyslang` versions.
Install
-
pip install pyslang
Imports
- slang
import pyslang
import slang
- Library
from pyslang import Library
from slang import Library
Quickstart
import slang
# 1. Create a library to manage SystemVerilog source files
library = slang.Library()
# 2. Add SystemVerilog source text. Multiple calls can add more files.
# You can also add files from disk using library.addSourceFile('path/to/file.sv')
sv_code = """
module MySimpleModule;
initial begin
$display("Hello from SystemVerilog in Python!");
end
endmodule;
"""
library.addSourceText(sv_code, fileName='example.sv')
# 3. Get the compiled representation (perform parsing and elaboration)
compilation = library.getCompilation()
# 4. Check for any diagnostics (errors or warnings)
diagnostics = compilation.getDiagnostics()
if diagnostics:
print("Compilation completed with diagnostics:")
for diag in diagnostics:
print(diag.toString())
else:
print("Compilation successful with no diagnostics.")
# Further operations (e.g., accessing symbols) would go here.
# For instance, finding the 'MySimpleModule' definition:
# root = compilation.getRoot()
# module = root.lookupName('MySimpleModule')