Skip to main content

Python bindings for slang, a library for compiling SystemVerilog

Project description

pyslang - Language bindings for slang, SystemVerilog parsing and compilation library

build PyPI License: MIT Join the chat at https://gitter.im/MikePopoloski/slang

slang is a software library that provides various components for lexing, parsing, type checking, and elaborating SystemVerilog code. pyslang exposes that library to Python projects.

Full documentation is available on the website: https://sv-lang.com

Installation

pyslang can be installed like any other Python library, using (a recent version of) the Python package manager pip, on Linux, macOS, and Windows:

pip install pyslang

or, to update your installed version to the latest release:

pip install -U pyslang

or, to checkout and install a local build:

git clone https://github.com/MikePopoloski/pyslang.git
cd pyslang
git submodule update --init --recursive
pip install .

Example usage

Given a 'test.sv' source file:

module memory(
    address,
    data_in,
    data_out,
    read_write,
    chip_en
  );

  input wire [7:0] address, data_in;
  output reg [7:0] data_out;
  input wire read_write, chip_en;

  reg [7:0] mem [0:255];

  always @ (address or data_in or read_write or chip_en)
    if (read_write == 1 && chip_en == 1) begin
      mem[address] = data_in;
  end

  always @ (read_write or chip_en or address)
    if (read_write == 0 && chip_en)
      data_out = mem[address];
    else
      data_out = 0;

endmodule

We can use slang to load the syntax tree and inspect it:

import pyslang

tree = pyslang.SyntaxTree.fromFile('test.sv')
mod = tree.root.members[0]
print(mod.header.name.value)
print(mod.members[0].kind)
print(mod.members[1].header.dataType)
memory
SyntaxKind.PortDeclaration
reg [7:0]

We can also evaluate arbitrary SystemVerilog expressions:

session = pyslang.ScriptSession()
session.eval("logic bit_arr [16] = '{0:1, 1:1, 2:1, default:0};")
result = session.eval("bit_arr.sum with ( int'(item) );")
print(result)
3

Contact & Support

If you encounter a bug, have questions, or want to contribute, please get in touch by opening a GitHub issue or posting a message on Gitter.

Contributions are welcome, whether they be in the form of bug reports, comments, suggestions, documentation improvements, or full fledged new features via pull requests.

License

slang (and pyslang) is licensed under the MIT license:

Copyright (c) 2015-2023 Michael Popoloski

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyslang-6.0.tar.gz (1.1 MB view hashes)

Uploaded Source

Built Distributions

pyslang-6.0-cp312-cp312-win_amd64.whl (2.2 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

pyslang-6.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

pyslang-6.0-cp312-cp312-macosx_10_15_x86_64.whl (2.9 MB view hashes)

Uploaded CPython 3.12 macOS 10.15+ x86-64

pyslang-6.0-cp312-cp312-macosx_10_15_universal2.whl (5.5 MB view hashes)

Uploaded CPython 3.12 macOS 10.15+ universal2 (ARM64, x86-64)

pyslang-6.0-cp311-cp311-win_amd64.whl (2.2 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

pyslang-6.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

pyslang-6.0-cp311-cp311-macosx_10_15_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.11 macOS 10.15+ x86-64

pyslang-6.0-cp311-cp311-macosx_10_15_universal2.whl (5.5 MB view hashes)

Uploaded CPython 3.11 macOS 10.15+ universal2 (ARM64, x86-64)

pyslang-6.0-cp310-cp310-win_amd64.whl (2.2 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

pyslang-6.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

pyslang-6.0-cp310-cp310-macosx_10_15_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.10 macOS 10.15+ x86-64

pyslang-6.0-cp310-cp310-macosx_10_15_universal2.whl (5.4 MB view hashes)

Uploaded CPython 3.10 macOS 10.15+ universal2 (ARM64, x86-64)

pyslang-6.0-cp39-cp39-win_amd64.whl (2.5 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

pyslang-6.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

pyslang-6.0-cp39-cp39-macosx_10_15_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.9 macOS 10.15+ x86-64

pyslang-6.0-cp39-cp39-macosx_10_15_universal2.whl (5.4 MB view hashes)

Uploaded CPython 3.9 macOS 10.15+ universal2 (ARM64, x86-64)

pyslang-6.0-cp38-cp38-win_amd64.whl (2.2 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

pyslang-6.0-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

pyslang-6.0-cp38-cp38-macosx_10_15_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.8 macOS 10.15+ x86-64

pyslang-6.0-cp38-cp38-macosx_10_15_universal2.whl (5.4 MB view hashes)

Uploaded CPython 3.8 macOS 10.15+ universal2 (ARM64, x86-64)

pyslang-6.0-cp37-cp37m-win_amd64.whl (2.2 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

pyslang-6.0-cp37-cp37m-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

pyslang-6.0-cp37-cp37m-macosx_10_15_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.7m macOS 10.15+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page