pyslang Python Bindings for Slang SystemVerilog Compiler

10.0.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `slang` library, add SystemVerilog source code, trigger compilation, and check for any errors or warnings. The `slang` library is primarily a compiler and analysis tool.

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')

view raw JSON →