Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Btor2Xcfa #348

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Empty file added .z3-trace
Empty file.
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ include(
"common/ltl-cli",

"frontends/c-frontend",
"frontends/btor2-frontend",
"frontends/petrinet-frontend/petrinet-model",
"frontends/petrinet-frontend/petrinet-analysis",
"frontends/petrinet-frontend/petrinet-xsts",
Expand All @@ -42,6 +43,7 @@ include(
"xcfa/xcfa",
"xcfa/cat",
"xcfa/c2xcfa",
"xcfa/btor2xcfa",
"xcfa/litmus2xcfa",
"xcfa/llvm2xcfa",
"xcfa/xcfa2chc",
Expand Down
1 change: 1 addition & 0 deletions subprojects/frontends/btor2-frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Soon...
24 changes: 24 additions & 0 deletions subprojects/frontends/btor2-frontend/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2025 Budapest University of Technology and Economics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id("kotlin-common")
id("antlr-grammar")
}
dependencies {
implementation(project(":theta-core"))
implementation(project(":theta-common"))
implementation(project(":theta-grammar"))
}
70 changes: 70 additions & 0 deletions subprojects/frontends/btor2-frontend/src/main/antlr/Btor2.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// WARNING: this grammar is NOT an official BTOR2 grammar and will accept invalid btor2 circuits.
// Check your circuit with catbtor before using this grammar!
grammar Btor2;

// Lexer rules
WS: [ ]+ -> skip;
NUM: [0-9]+;
PLUS: '+';
MINUS: '-';
UNARYOP: 'not'
| 'inc' | 'dec' | 'neg'
| 'redand' | 'redor' | 'redxor';
TERNARYOP: 'ite' | 'write';
BINOP: 'and' | 'nand' | 'nor' | 'or' | 'xor' | 'iff' | 'implies'
| 'eq' | 'neq'
| 'slt' | 'slte' | 'sgt' | 'sgte'
| 'ult' | 'ulte' | 'ugt' | 'ugte'
| 'concat' | 'add' | 'mul'
| 'udiv' | 'urem'
| 'sdiv' | 'srem' | 'smod'
| 'saddo' | 'sdivo' | 'smulo' | 'ssubo'
| 'uaddo' | 'umulo' | 'usubo'
| 'rol' | 'ror' | 'sll' | 'sra' | 'srl' | 'read';
SYMBOL: ~[ \r\n]+;
COMMENT: ';' ~[\r\n]+;

// Parser rules
btor2: (line '\n')* line ('\n')*;

line: comment | node (symbol)? (comment)?;

comment: COMMENT;

nid: NUM;
sid: NUM;

node: ( array_sort | bitvec_sort ) #sort // sort declaration
| (input | state | init | next | property) #stateful
| (opidx | op) #operation
| (filled_constant | constant | constant_d | constant_h) #constantNode;

opidx: ext | slice;

ext: id=nid ('uext'|'sext') sid opd1=nid w=NUM;
slice: id=nid 'slice' sid opd1=nid u=NUM l=NUM;

op: binop | unop | terop;

binop: id=nid BINOP sid opd1=nid opd2=nid;
unop: id=nid UNARYOP sid opd1=nid;
terop: id=nid TERNARYOP sid opd1=nid opd2=nid opd3=nid;

input: id=nid ('input') sid;

init: id=nid 'init' sid param1=nid param2=nid;
next: id=nid 'next' sid param1=nid param2=nid;

state: id=nid 'state' sid;

property: id=nid property_type=('bad' | 'constraint' | 'fair' | 'output' | 'justice' ) param=nid;

array_sort: id=sid 'sort array' sid1=sid sid2=sid;
bitvec_sort: id=sid 'sort bitvec' width=NUM;

constant: id=nid 'const' sid bin=NUM;
constant_d: id=nid 'constd' sid (MINUS)? dec=NUM;
constant_h: id=nid 'consth' sid hex=SYMBOL;
filled_constant: id=nid fill=('one' | 'ones' | 'zero') sid;

symbol: SYMBOL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2025 Budapest University of Technology and Economics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package hu.bme.mit.theta.frontend.models

import hu.bme.mit.theta.core.decl.VarDecl
import hu.bme.mit.theta.core.type.Expr
import hu.bme.mit.theta.core.type.bvtype.BvLitExpr

data class Btor2Const(override val nid: UInt, val value: BooleanArray, override val sort: Btor2Sort) : Btor2Node(nid, sort)
{
override fun getVar(): VarDecl<*>? {
return null
}

override fun getExpr(): Expr<*> {
return BvLitExpr.of(value)
}

override fun <R, P> accept(visitor: Btor2NodeVisitor<R, P>, param : P): R {
return visitor.visit(this, param)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2025 Budapest University of Technology and Economics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package hu.bme.mit.theta.frontend.models

import hu.bme.mit.theta.core.decl.VarDecl
import hu.bme.mit.theta.core.type.Expr

public interface Btor2NodeVisitor<R, P> {
fun visit(node: Btor2UnaryOperation, param: P) : R
fun visit(node: Btor2BinaryOperation, param: P) : R
fun visit(node: Btor2TernaryOperation, param: P) : R
fun visit(node: Btor2SliceOperation, param: P) : R
fun visit(node: Btor2ExtOperation, param: P) : R
fun visit(node: Btor2Comparison, param: P) : R
fun visit(node: Btor2Const, param: P) : R
fun visit(node: Btor2BitvecSort, param: P) : R
fun visit(node: Btor2Input, param: P) : R
fun visit(node: Btor2State, param: P) : R
fun visit(node: Btor2Init, param: P) : R
fun visit(node: Btor2Next, param: P) : R
fun visit(node: Btor2Bad, param: P) : R
fun visit(node: Btor2Constraint, param: P) : R
fun visit(node: Btor2Fair, param: P) : R
fun visit(node: Btor2Output, param: P) : R
}


object Btor2Circuit {
var nodes: MutableMap<UInt, Btor2Node> = mutableMapOf()
var sorts: MutableMap<UInt, Btor2Sort> = mutableMapOf()
var constants: MutableMap<UInt, Btor2Const> = mutableMapOf()
var ops: MutableMap<UInt, Btor2Operation> = mutableMapOf()
var states: MutableMap<UInt, Btor2Stateful> = mutableMapOf()
var properties : MutableMap<UInt, Btor2Bad> = mutableMapOf()
}

// sortID lookup in Btor2Sort
abstract class Btor2Node(id: UInt, btor2Sort: Btor2Sort? = null) {
abstract val nid: UInt
abstract val sort: Btor2Sort?

open fun getVar(): VarDecl<*>? {
return null
}

abstract fun <R, P> accept(visitor: Btor2NodeVisitor<R, P>, param : P): R
abstract fun getExpr(): Expr<*>

}

abstract class Btor2Sort(sid: UInt, width: UInt) {
abstract val sid: UInt
abstract val width: UInt
}

data class Btor2BitvecSort(override val sid: UInt, override val width: UInt) : Btor2Sort(sid, width)
Loading