-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add encoding for dynamic memory layouts.
- Loading branch information
1 parent
ba29901
commit 3a60ebf
Showing
2 changed files
with
178 additions
and
6 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
dartagnan/src/main/java/com/dat3m/dartagnan/encoding/EncodingHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.dat3m.dartagnan.encoding; | ||
|
||
import com.google.common.base.Preconditions; | ||
import org.sosy_lab.java_smt.api.*; | ||
|
||
import java.math.BigInteger; | ||
|
||
public class EncodingHelper { | ||
|
||
private final FormulaManager fmgr; | ||
|
||
public EncodingHelper(FormulaManager fmgr) { | ||
this.fmgr = fmgr; | ||
} | ||
|
||
public BooleanFormula equals(Formula left, Formula right) { | ||
if (left instanceof NumeralFormula.IntegerFormula iLeft && right instanceof NumeralFormula.IntegerFormula iRight) { | ||
return fmgr.getIntegerFormulaManager().equal(iLeft, iRight); | ||
} | ||
|
||
if (left instanceof BitvectorFormula bvLeft && right instanceof BitvectorFormula bvRight) { | ||
final BitvectorFormulaManager bvmgr = fmgr.getBitvectorFormulaManager(); | ||
Preconditions.checkState(bvmgr.getLength(bvLeft) == bvmgr.getLength(bvRight)); | ||
return fmgr.getBitvectorFormulaManager().equal(bvLeft, bvRight); | ||
} | ||
|
||
throw new UnsupportedOperationException("Mismatching types: " + left + " and " + right); | ||
} | ||
|
||
public BooleanFormula greaterThan(Formula left, Formula right, boolean signed) { | ||
if (left instanceof NumeralFormula.IntegerFormula iLeft && right instanceof NumeralFormula.IntegerFormula iRight) { | ||
return fmgr.getIntegerFormulaManager().greaterThan(iLeft, iRight); | ||
} | ||
|
||
if (left instanceof BitvectorFormula bvLeft && right instanceof BitvectorFormula bvRight) { | ||
final BitvectorFormulaManager bvmgr = fmgr.getBitvectorFormulaManager(); | ||
Preconditions.checkState(bvmgr.getLength(bvLeft) == bvmgr.getLength(bvRight)); | ||
return fmgr.getBitvectorFormulaManager().greaterThan(bvLeft, bvRight, signed); | ||
} | ||
|
||
throw new UnsupportedOperationException("Mismatching types: " + left + " and " + right); | ||
} | ||
|
||
public BooleanFormula greaterOrEquals(Formula left, Formula right, boolean signed) { | ||
if (left instanceof NumeralFormula.IntegerFormula iLeft && right instanceof NumeralFormula.IntegerFormula iRight) { | ||
return fmgr.getIntegerFormulaManager().greaterOrEquals(iLeft, iRight); | ||
} | ||
|
||
if (left instanceof BitvectorFormula bvLeft && right instanceof BitvectorFormula bvRight) { | ||
final BitvectorFormulaManager bvmgr = fmgr.getBitvectorFormulaManager(); | ||
Preconditions.checkState(bvmgr.getLength(bvLeft) == bvmgr.getLength(bvRight)); | ||
return fmgr.getBitvectorFormulaManager().greaterOrEquals(bvLeft, bvRight, signed); | ||
} | ||
|
||
throw new UnsupportedOperationException("Mismatching types: " + left + " and " + right); | ||
} | ||
|
||
public Formula add(Formula left, Formula right) { | ||
if (left instanceof NumeralFormula.IntegerFormula iLeft && right instanceof NumeralFormula.IntegerFormula iRight) { | ||
return fmgr.getIntegerFormulaManager().add(iLeft, iRight); | ||
} | ||
|
||
if (left instanceof BitvectorFormula bvLeft && right instanceof BitvectorFormula bvRight) { | ||
final BitvectorFormulaManager bvmgr = fmgr.getBitvectorFormulaManager(); | ||
Preconditions.checkState(bvmgr.getLength(bvLeft) == bvmgr.getLength(bvRight)); | ||
return fmgr.getBitvectorFormulaManager().add(bvLeft, bvRight); | ||
} | ||
|
||
throw new UnsupportedOperationException("Mismatching types: " + left + " and " + right); | ||
} | ||
|
||
public Formula subtract(Formula left, Formula right) { | ||
if (left instanceof NumeralFormula.IntegerFormula iLeft && right instanceof NumeralFormula.IntegerFormula iRight) { | ||
return fmgr.getIntegerFormulaManager().subtract(iLeft, iRight); | ||
} | ||
|
||
if (left instanceof BitvectorFormula bvLeft && right instanceof BitvectorFormula bvRight) { | ||
final BitvectorFormulaManager bvmgr = fmgr.getBitvectorFormulaManager(); | ||
Preconditions.checkState(bvmgr.getLength(bvLeft) == bvmgr.getLength(bvRight)); | ||
return fmgr.getBitvectorFormulaManager().subtract(bvLeft, bvRight); | ||
} | ||
|
||
throw new UnsupportedOperationException("Mismatching types: " + left + " and " + right); | ||
} | ||
|
||
public Formula remainder(Formula left, Formula right) { | ||
if (left instanceof NumeralFormula.IntegerFormula iLeft && right instanceof NumeralFormula.IntegerFormula iRight) { | ||
// NOTE: This is not the same as the BV version in terms of signedness (here the sign is from the numerator) | ||
return fmgr.getIntegerFormulaManager().modulo(iLeft, iRight); | ||
} | ||
|
||
if (left instanceof BitvectorFormula bvLeft && right instanceof BitvectorFormula bvRight) { | ||
final BitvectorFormulaManager bvmgr = fmgr.getBitvectorFormulaManager(); | ||
Preconditions.checkState(bvmgr.getLength(bvLeft) == bvmgr.getLength(bvRight)); | ||
return fmgr.getBitvectorFormulaManager().remainder(bvLeft, bvRight, true); | ||
} | ||
|
||
throw new UnsupportedOperationException("Mismatching types: " + left + " and " + right); | ||
} | ||
|
||
public Formula value(BigInteger value, FormulaType<?> type) { | ||
if (type.isIntegerType()) { | ||
return fmgr.getIntegerFormulaManager().makeNumber(value); | ||
} else if (type.isBitvectorType()) { | ||
int size = getBitSize(type); | ||
return fmgr.getBitvectorFormulaManager().makeBitvector(size, value); | ||
} | ||
throw new UnsupportedOperationException("Cannot generate value of type " + type); | ||
} | ||
|
||
public int getBitSize(FormulaType<?> type) { | ||
if (type.isIntegerType()) { | ||
return -1; | ||
} else if (type.isBitvectorType()) { | ||
return ((FormulaType.BitvectorType)type).getSize(); | ||
} | ||
throw new UnsupportedOperationException("Cannot get bit-size for type " + type); | ||
} | ||
|
||
public FormulaType<?> typeOf(Formula formula) { | ||
return fmgr.getFormulaType(formula); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters