Very basic btor2 parser, circuit miter, and code optimizer.
Project description
btor2-opt
Very basic btor2 parser, circuit miter, and code optimizer.
Install
pip install btor2-opt
Overview
This repo contains two main scripts:
btor2-opt
: Takes a.btor2
file and a list of pass names as argument and prints out the transformed result.btor2-miter
: Takes a.fir
file as input and runs it throughfirrtl
andfirtool
to obtain two.btor2
designs which are then merged into a single miter circuit before being returned to the user. Note that this requires havingfirrtl
andfirtool
in your path, specifically a version offirtool
that has the--btor2
flag.
The rest of the code can be found in the src
folder, which contains a basic parser for btor2
(not entirely complete, but supports anything firtool --btor2
can produce), an internal representation of the language and a simple pass infrastructure, where you can add any of you custom passes.
Adding a Pass
Simply create a new class (as its own file) in src/passes
that inherits from Pass
. Then in the constructor, make sure you give it a name. The pass's logic itself is written by overiding the run(p: list[Instruction]) -> list[Instruction]
method. The pass must then be imported in src/passes/passes.py
and instantiated in the all_passes
list. Passes are grouped either in transforms
, which contain all of the passes that transform the AST, and validation
, which contains all of the passes used to gurantee the syntactic correctness of the output program.
Here is a simple example pass that renames all inputs to "inp_n".
# Example pass: Simply renames all inputs to inp_<pos>
class RenameInputs(Pass):
def __init__(self):
super().__init__("rename-inputs")
# I chose to have this pass not modify p in place
# you can also simply modify p and return it
def run(p: list[Instruction]) -> list[Instruction]:
i = 0
res = []
for inst in p:
if isinstance(inst, Input):
res.append(Input(inst.lid, inst.sort, f"inp_{i}"))
i += 1
else:
res.append(inst)
return res
# Make sure to add an instance of the pass to the all_passes array
all_passes = [RenameInputs()]
This pass can then be called by running:
python3 btor2-opt.py ex.btor2 rename-inputs
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file btor2_opt-0.1.3.tar.gz
.
File metadata
- Download URL: btor2_opt-0.1.3.tar.gz
- Upload date:
- Size: 46.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e42fef63feaf175b974250a18644b4049f7f81572eb308301f1b71f0c9d4676 |
|
MD5 | 3ac04f9d7e8f26087d378910e5b271ed |
|
BLAKE2b-256 | e3173b71b41929fbc4755d0b7d7814a0ca93b08101dbd89dcf5236961102807e |
File details
Details for the file btor2_opt-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: btor2_opt-0.1.3-py3-none-any.whl
- Upload date:
- Size: 39.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12cb9dd5876bdd863057b1e931439e8738b874f7afb8562ae88bd9ac44baaa31 |
|
MD5 | a909755c4bb76c736f7c7edb779e82f9 |
|
BLAKE2b-256 | f7599888914c0658e4e687ff90d072fca202b9a4dcfa2451134a4633b3734370 |