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
For nix users
nix-shell -p 'python3.withPackages(ps: with ps; [ black build bumpver click colorama isort lexid looseversion mypy-extensions packaging pathspec pip-tools platformdirs pyproject-hooks toml tomli typing-extensions wheel])'
Test
Unit tests can be run using:
python -m unittest tests/test.py
Overview
This repo contains two main scripts:
btoropt: Takes a.btor2file and a list of pass names as argument and prints out the transformed result.btormiter: Takes a.firfile as input and runs it throughfirrtlandfirtoolto obtain two.btor2designs which are then merged into a single miter circuit before being returned to the user. Note that this requires havingfirrtlandfirtoolin your path, specifically a version offirtoolthat has the--btor2flag.
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.
Supported Instructions
This compiler currently supports the following btor2 instructions:
| Instruction | Description |
|---|---|
<lid> sort <type> <width> |
Declares a type |
<lid> input <sid> <name> |
Declares an input |
<lid> output <out> |
Declares an output |
<lid> bad <cond> |
Checks the inversion of a condition |
<lid> constraint <cond> |
Assumes a condition |
<lid> zero <sid> |
Declares a 0 constant |
<lid> one <sid> |
Declares a 1 constant |
<lid> ones <sid> |
Declares a bit-string of 1s |
<lid> not <sid> <cond> |
Negates a condition |
<lid> constd <sid> <val> |
Declares a decimal constant |
<lid> consth <sid> <val> |
Declares a hexadecimal constant |
<lid> const <sid> <val> |
Declares a binary constant |
<lid> state <sid> <name> |
Declares a stateful element |
<lid> init <sid> <state> <val> |
Initializes a state |
<lid> next <sid> <state> <next> |
Sets the transition logic of a state |
<lid> slice <sid> <op> <hb> <lb> |
Extracts bits [hb:lb] from a result |
<lid> ite <sid> <cond> <t> <f> |
If-then-else expression |
<lid> implies <sid> <lhs> <rhs> |
Logical implication |
<lid> iff <sid> <lhs> <rhs> |
If and only if expression |
<lid> add/sub/mul <sid> <l> <r> |
Binary operation |
<lid> {s,u}div <sid> <l> <r> |
Signed or unsigned division |
<lid> smod <sid> <l> <r> |
Signed modulo |
<lid> s{l,r}l <sid> <l> <r> |
Logical shift left/right |
<lid> sra <sid> <l> <r> |
Arithmetic shift right |
<lid> and/or/xor <sid> <l> <r> |
Binary logical operators |
<lid> concat <sid> <l> <r> |
Concatenate two results |
<lid> eq/neq <sid> <l> <r> |
Equality comparators |
<lid> {s,u}gt <sid> <l> <r> |
Signed/Unsigned l > r |
<lid> {s,u}gte <sid> <l> <r> |
Signed/Unsigned l ≥ r |
<lid> {s,u}lt <sid> <l> <r> |
Signed/Unsigned l < r |
<lid> {s,u}lte <sid> <l> <r> |
Signed/Unsigned l ≤ r |
<lid> uext <sid> <opid> <w> <name> |
Unsigned width extension / aliasing |
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(self, 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:
btoropt ex.btor2 rename-inputs
Custom BTOR2 Extensions
btoropt currently supports custom extensions to the standard btor2 format, enabling the expression of modularity.
In order to maintin inter-operability with standard btor2 files, this extension must be explicitly enabled using the --modular flag.
These extensions are simply syntactic sugar to enable parallelism and can be lowered to standard btor2.
This makes it possible to express the following custom structures:
| Structure | Description |
|---|---|
module <name> {...} |
Declares a named region of instructions |
contract <module_name> {...} |
Declares a contract for a module |
Module bodies support all standard btor2 along with the following custom instructions:
| Module Instructions | Description |
|---|---|
<lid> ref <module_name> <lid_in_module> |
References an instruction from another module |
<lid> inst <module_name> |
Creates an instance of a module |
<lid> set <instance_lid> <ref_lid> <local_lid> |
Sets an instance reference to a local instruction |
Contract bodies support all standard btor2 along with the following custom instructions:
| Contract Instructions | Description |
|---|---|
<lid> ref <module_name> <lid_in_module> |
References an instruction from the contract's module |
<lid> prec <cond_lid> |
Declares a precondition |
<lid> post <cond_lid> |
Declares a postcondition |
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file btor2_opt-0.3.0.tar.gz.
File metadata
- Download URL: btor2_opt-0.3.0.tar.gz
- Upload date:
- Size: 55.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2d1c0c947b8bdf5c70d9726e723452984952fa5e490a662167983b4fe7b2a96
|
|
| MD5 |
24a57911931d245c010226eeb3566aba
|
|
| BLAKE2b-256 |
55a7906a056e8d449b4bad49469bb146bb3274a07ae4de86348fbaaf5c42d51a
|
File details
Details for the file btor2_opt-0.3.0-py3-none-any.whl.
File metadata
- Download URL: btor2_opt-0.3.0-py3-none-any.whl
- Upload date:
- Size: 47.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e77bc4f5fef4ebb7b2e27c44ea52d9364d3f88920c73e33b5ab1b6640b0edf8c
|
|
| MD5 |
c442cf355d9784bab127f3c0ee8f824c
|
|
| BLAKE2b-256 |
6bd496e3abbb8654cd363019b4ae7b1c2e054cda072325691b493cda2d5bb1ca
|