From db3479b37b715bd01afa7bf82baf50c46e23b3f2 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Tue, 9 Sep 2025 13:58:27 +0200 Subject: [PATCH 01/33] Added rudimentary pointer expressions --- .../dartagnan/encoding/ExpressionEncoder.java | 94 ++++++++++++++++++- .../expression/ExpressionVisitor.java | 6 ++ .../expression/pointer/IntToPtrCast.java | 21 +++++ .../expression/pointer/NullLiteral.java | 28 ++++++ .../expression/pointer/PointerAddExpr.java | 41 ++++++++ .../expression/pointer/PointerCmpOp.java | 25 +++++ .../expression/pointer/PtrCmpExpr.java | 21 +++++ .../expression/pointer/PtrToIntCast.java | 21 +++++ .../expression/type/PointerType.java | 20 ++++ 9 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/IntToPtrCast.java create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/NullLiteral.java create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpExpr.java create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index 22458ee63e..c9cb336bce 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -11,6 +11,7 @@ import com.dat3m.dartagnan.expression.booleans.BoolUnaryOp; import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.ITEExpr; +import com.dat3m.dartagnan.expression.pointer.*; import com.dat3m.dartagnan.expression.type.*; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; import com.dat3m.dartagnan.program.Register; @@ -178,6 +179,15 @@ public void setEvent(Event e) { return (TypedFormula) typedFormula; } + @SuppressWarnings("unchecked") + public TypedFormula encodePointerExpr(Expression expression) { + checkArgument(expression.getType() instanceof PointerType); + final TypedFormula typedFormula = encode(expression); + assert typedFormula.type() == expression.getType(); + assert typedFormula.formula() instanceof IntegerFormula || typedFormula.formula() instanceof BitvectorFormula; + return (TypedFormula) typedFormula; + } + @SuppressWarnings("unchecked") public TypedFormula encodeBooleanExpr(Expression expression) { Preconditions.checkArgument(expression.getType() instanceof BooleanType); @@ -492,6 +502,87 @@ public TypedFormula visitIntCmpExpression(IntCmpExp return new TypedFormula<>(expr.getType(), enc); } + + // ==================================================================================== + // Pointers + + @Override + public TypedFormula visitPointerAddExpression(PointerAddExpr expr) { + final TypedFormula base = encodePointerExpr(expr.getBase()); + final TypedFormula offset = encodeIntegerExpr(expr.getOffset()); + + if (context.useIntegers) { + final IntegerFormula baseForm = (IntegerFormula) base.formula(); + final IntegerFormula offsetForm = (IntegerFormula) offset.formula(); + + return new TypedFormula<>(base.getType(), integerFormulaManager().add(baseForm, offsetForm)); + } else { + final BitvectorFormula baseForm = (BitvectorFormula) base.formula(); + final BitvectorFormula offsetForm = (BitvectorFormula) offset.formula(); + + return new TypedFormula<>(base.getType(), bitvectorFormulaManager().add(baseForm, offsetForm)); + } + + } + + @Override + public TypedFormula visitPtrToIntCastExpression(PtrToIntCast expr) { + final TypedFormula ptr = encodePointerExpr(expr.getOperand()); + return new TypedFormula<>(expr.getType(), ptr.formula()); + + } + + @Override + public TypedFormula visitIntToPtrCastExpression(IntToPtrCast expr) { + final TypedFormula address = encodeIntegerExpr(expr.getOperand()); + return new TypedFormula<>(expr.getType(), address.formula()); + } + + @Override + public TypedFormula visitPtrCmpExpression(PtrCmpExpr expr) { + if (context.useIntegers) { + final var left = (TypedFormula) encodePointerExpr(expr.getLeft()); + final var right = (TypedFormula) encodePointerExpr(expr.getRight()); + + final IntegerFormulaManager imgr = integerFormulaManager(); + final BooleanFormula result = switch (expr.getKind()) { + case EQ -> imgr.equal(left.formula(), right.formula()); + case NEQ -> bmgr.not(fmgr.equal(left.formula(), right.formula())); + case LT -> imgr.lessThan(left.formula(), right.formula()); + case LTE -> imgr.lessOrEquals(left.formula(), right.formula()); + case GT -> imgr.greaterThan(left.formula(), right.formula()); + case GTE -> imgr.greaterOrEquals(left.formula(), right.formula()); + }; + return new TypedFormula<>(types.getBooleanType(), result); + }else{ + final var left = (TypedFormula) encodePointerExpr(expr.getLeft()); + final var right = (TypedFormula) encodePointerExpr(expr.getRight()); + final BitvectorFormulaManager bvgr = bitvectorFormulaManager(); + final BooleanFormula result = switch (expr.getKind()) { + case EQ -> bvgr.equal(left.formula(), right.formula()); + case NEQ -> bmgr.not(fmgr.equal(left.formula(), right.formula())); + case LT -> bvgr.lessThan(left.formula(), right.formula(),false); + case LTE -> bvgr.lessOrEquals(left.formula(), right.formula(),false); + case GT -> bvgr.greaterThan(left.formula(), right.formula(),false); + case GTE -> bvgr.greaterOrEquals(left.formula(), right.formula(),false); + }; + return new TypedFormula<>(types.getBooleanType(), result); + } + } + + @Override + public TypedFormula visitNullLiteral(NullLiteral lit) { + final Formula zero = context.useIntegers + ? integerFormulaManager().makeNumber(0) + : bitvectorFormulaManager().makeBitvector(types.getArchType().getBitWidth(), 0); + + return new TypedFormula<>(lit.getType(), zero); + } + + + + + // ==================================================================================== // Aggregates @@ -562,7 +653,8 @@ public TypedFormula visitInsertExpression(InsertExpr insert) { @Override public TypedFormula visitMemoryObject(MemoryObject memObj) { - return makeVariable(String.format("addrof(%s)", memObj), memObj.getType()); + Formula result = makeVariable(String.format("addrof(%s)", memObj), memObj.getType()).formula(); + return new TypedFormula<>(memObj.getType(), result); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index 3396f90063..cd7c728fc0 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -11,6 +11,7 @@ import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.GEPExpr; import com.dat3m.dartagnan.expression.misc.ITEExpr; +import com.dat3m.dartagnan.expression.pointer.*; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.memory.FinalMemoryValue; @@ -57,6 +58,11 @@ public interface ExpressionVisitor { // =================================== Pointer =================================== default TRet visitGEPExpression(GEPExpr expr) { return visitExpression(expr); } + default TRet visitPointerAddExpression(PointerAddExpr expr) { return visitExpression(expr); } + default TRet visitIntToPtrCastExpression(IntToPtrCast expr) { return visitCastExpression(expr); } + default TRet visitPtrToIntCastExpression(PtrToIntCast expr) { return visitCastExpression(expr); } + default TRet visitPtrCmpExpression(PtrCmpExpr expr) { return visitBinaryExpression(expr); } + default TRet visitNullLiteral(NullLiteral lit) { return visitLeafExpression(lit); } // =================================== Generic =================================== default TRet visitITEExpression(ITEExpr expr) { return visitExpression(expr); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/IntToPtrCast.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/IntToPtrCast.java new file mode 100644 index 0000000000..f2064b2c1b --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/IntToPtrCast.java @@ -0,0 +1,21 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.Expression; +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.CastExpressionBase; +import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; +import com.dat3m.dartagnan.expression.utils.ExpressionHelper; + +public final class IntToPtrCast extends CastExpressionBase { + + public IntToPtrCast(PointerType pointerType, Expression operand) { + super(pointerType, operand); + ExpressionHelper.checkExpectedType(operand, IntegerType.class); + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitIntToPtrCastExpression(this); + } +} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/NullLiteral.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/NullLiteral.java new file mode 100644 index 0000000000..f179a82e1f --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/NullLiteral.java @@ -0,0 +1,28 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.ExpressionKind; +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.LeafExpressionBase; +import com.dat3m.dartagnan.expression.type.PointerType; + +public class NullLiteral extends LeafExpressionBase { + + public NullLiteral(PointerType type) { + super(type); + } + + @Override + public ExpressionKind getKind() { + return () -> "NULL"; + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitNullLiteral(this); + } + + @Override + public String toString() { + return "nullptr"; + } +} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java new file mode 100644 index 0000000000..0fee5afe20 --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java @@ -0,0 +1,41 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.Expression; +import com.dat3m.dartagnan.expression.ExpressionKind; +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.ExpressionBase; +import com.dat3m.dartagnan.expression.type.PointerType; +import com.dat3m.dartagnan.expression.type.TypeFactory; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; + +public class PointerAddExpr extends ExpressionBase { + + private final Expression base; + private final Expression offset; + + public PointerAddExpr(Expression base, Expression offset) { + super((PointerType) base.getType()); + Preconditions.checkArgument(offset.getType().equals(TypeFactory.getInstance().getArchType())); + this.base = base; + this.offset = offset; + } + + public Expression getBase() { return base; } + public Expression getOffset() { return offset; } + + @Override + public ImmutableList getOperands() { + return ImmutableList.of(base, offset); + } + + @Override + public ExpressionKind getKind() { + return () -> "ptradd"; + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitPointerAddExpression(this); + } +} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java new file mode 100644 index 0000000000..0e16c29e54 --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java @@ -0,0 +1,25 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.ExpressionKind; + +public enum PointerCmpOp implements ExpressionKind { + EQ, NEQ, GTE, LTE, GT, LT; + + @Override + public String toString() { + return getSymbol(); + } + + @Override + public String getSymbol() { + return switch (this) { + case EQ -> "=="; + case NEQ -> "!="; + case GTE -> ">="; + case LTE -> "<="; + case GT -> ">"; + case LT -> "<"; + }; + } +} + diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpExpr.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpExpr.java new file mode 100644 index 0000000000..8b2a392847 --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpExpr.java @@ -0,0 +1,21 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.Expression; +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.BinaryExpressionBase; +import com.dat3m.dartagnan.expression.type.BooleanType; +import com.dat3m.dartagnan.expression.type.PointerType; +import com.dat3m.dartagnan.expression.utils.ExpressionHelper; + +public final class PtrCmpExpr extends BinaryExpressionBase { + + public PtrCmpExpr(BooleanType type, Expression left, PointerCmpOp kind, Expression right) { + super(type, kind, left, right); + ExpressionHelper.checkSameExpectedType(left, right, PointerType.class); + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitPtrCmpExpression(this); + } +} \ No newline at end of file diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java new file mode 100644 index 0000000000..97a30b1e59 --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java @@ -0,0 +1,21 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.Expression; +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.CastExpressionBase; +import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; +import com.dat3m.dartagnan.expression.utils.ExpressionHelper; + +public final class PtrToIntCast extends CastExpressionBase { + + public PtrToIntCast(IntegerType archType, Expression operand) { + super(archType, operand); + ExpressionHelper.checkExpectedType(operand, PointerType.class); + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitPtrToIntCastExpression(this); + } +} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java new file mode 100644 index 0000000000..df927c90c8 --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -0,0 +1,20 @@ +package com.dat3m.dartagnan.expression.type; + +import com.dat3m.dartagnan.expression.Type; + +public class PointerType implements Type { + + PointerType() { + } + + @Override + public String toString() { + return "ptr"; + } + + @Override + public boolean equals(Object obj) { + return obj != null && obj.getClass() == this.getClass(); + } + +} From b0d6bef1734fc30b0adcf71022f666b043639369 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 15 Sep 2025 15:12:20 +0200 Subject: [PATCH 02/33] fixed expr processing --- .../dartagnan/encoding/ExpressionEncoder.java | 4 ++ .../dartagnan/encoding/ProgramEncoder.java | 7 ++- .../expression/ExpressionFactory.java | 59 +++++++++++++++++-- .../expression/ExpressionPrinter.java | 17 ++++++ .../dartagnan/expression/misc/GEPExpr.java | 3 +- .../processing/ExprTransformer.java | 32 ++++++++++ .../expression/type/PointerType.java | 12 +++- .../expression/type/TypeFactory.java | 17 ++++-- .../parsers/program/visitors/VisitorLlvm.java | 12 ++-- .../dartagnan/program/event/EventFactory.java | 4 +- .../dartagnan/program/memory/Memory.java | 3 +- .../program/memory/MemoryObject.java | 7 ++- .../program/memory/VirtualMemoryObject.java | 3 +- .../program/processing/GEPToAddition.java | 4 +- .../program/processing/Intrinsics.java | 2 +- 15 files changed, 154 insertions(+), 32 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index c9cb336bce..bf205c540e 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -93,6 +93,10 @@ public TypedFormula encodeBooleanFinal(Expression e variable = context.useIntegers ? integerFormulaManager().makeVariable(name) : bitvectorFormulaManager().makeVariable(integerType.getBitWidth(), name); + }else if (type instanceof PointerType) { + variable = context.useIntegers + ? integerFormulaManager().makeVariable(name) + : bitvectorFormulaManager().makeVariable(types.getArchType().getBitWidth(), name); } else if (type instanceof AggregateType aggType) { final List fields = new ArrayList<>(aggType.getFields().size()); for (TypeOffset field : aggType.getFields()) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ProgramEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ProgramEncoder.java index b0a6a22432..ef9746f08e 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ProgramEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ProgramEncoder.java @@ -426,7 +426,7 @@ private BooleanFormula encodeMemoryLayout(Memory memory) { final List memoryObjects = ImmutableList.copyOf(memory.getObjects()); for (int i = 0; i < memoryObjects.size(); i++) { final MemoryObject cur = memoryObjects.get(i); - final Expression addrVar = context.address(cur); + final Expression addrVar = exprs.makePtrToIntCast(context.address(cur)); final Expression sizeVar = context.size(cur); final Expression size; @@ -459,7 +459,10 @@ private BooleanFormula encodeMemoryLayout(Memory memory) { // First object is placed at alignment enc.add(equate.apply(addrVar, alignment)); } else { - final Expression nextAvailableAddr = exprs.makeAdd(context.address(prev), context.size(prev)); + final Expression nextAvailableAddr = exprs.makeAdd( + exprs.makePtrToIntCast(context.address(prev)), + context.size(prev) + ); final Expression nextAlignedAddr = exprs.makeAdd(nextAvailableAddr, exprs.makeSub(alignment, exprs.makeRem(nextAvailableAddr, alignment, true)) ); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index f2210f1445..c40699e884 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -6,6 +6,7 @@ import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.GEPExpr; import com.dat3m.dartagnan.expression.misc.ITEExpr; +import com.dat3m.dartagnan.expression.pointer.*; import com.dat3m.dartagnan.expression.type.*; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; import com.dat3m.dartagnan.program.memory.MemoryObject; @@ -201,6 +202,8 @@ public Expression makeIntegerCast(Expression operand, IntegerType targetType, bo return sourceType.equals(targetType) ? operand : new IntSizeCast(targetType, operand, signed); } else if (sourceType instanceof FloatType) { return new FloatToIntCast(targetType, operand, signed); + }else if (sourceType instanceof PointerType) { + return new PtrToIntCast(targetType, operand); } throw new UnsupportedOperationException(String.format("Cannot cast %s to %s.", sourceType, targetType)); @@ -315,20 +318,23 @@ public Expression makeAggregateCmp(Expression x, AggregateCmpOp op, Expression y // Pointers public Expression makeGetElementPointer(Type indexingType, Expression base, List offsets) { - return makeGetElementPointer(indexingType, base, offsets, null); + Preconditions.checkArgument(base.getType().equals(types.getPointerType()), + "Applying offsets to non-pointer expression."); + return new GEPExpr(indexingType, base, offsets,null); } public Expression makeGetElementPointer(Type indexingType, Expression base, List offsets, Integer stride) { // TODO: Stride should be a property of the pointer, not of a GEPExpr. // Refactor GEPExpr to only accept a (new) PointerType and a list of offsets. // A PointerType should have the referred type and the stride in its attributes. - Preconditions.checkArgument(base.getType().equals(types.getArchType()), + Preconditions.checkArgument(base.getType().equals(types.getPointerType()), "Applying offsets to non-pointer expression."); Preconditions.checkArgument(stride == null || stride >= types.getMemorySizeInBytes(indexingType), "Stride cannot be smaller than indexing type"); return new GEPExpr(indexingType, base, offsets, stride); } + public ScopedPointer makeScopedPointer(String id, ScopedPointerType type, Expression value) { return new ScopedPointer(id, type, value); } @@ -337,6 +343,35 @@ public ScopedPointerVariable makeScopedPointerVariable(String id, ScopedPointerT return new ScopedPointerVariable(id, type, memObj); } + public Expression makePtrAdd(Expression base, Expression offset) { + return new PointerAddExpr(base, offset); + } + + + public Expression makePtrToIntCast(Expression pointer) { + return new PtrToIntCast(types.getArchType(), pointer); + } + + public Expression makeIntToPtrCast(Expression operand, PointerType pointerType) { + return new IntToPtrCast(pointerType, operand); + } + + public Expression makeIntToPtrCast(Expression operand) { + return new IntToPtrCast(types.getPointerType(), operand); + } + + public Expression makeNullLiteral(PointerType pointerType) { + return new NullLiteral(pointerType); + } + + public Expression makeNullLiteral() { + return makeNullLiteral(types.getPointerType()); + } + + public Expression makePtrCmp(Expression left, PointerCmpOp op, Expression right) { + return new PtrCmpExpr(types.getBooleanType(), left, op, right); + } + // ----------------------------------------------------------------------------------------------------------------- // Misc @@ -360,7 +395,9 @@ public Expression makeGeneralZero(Type type) { return makeFalse(); } else if (type instanceof FloatType floatType) { return makeZero(floatType); - } else { + } else if (type instanceof PointerType pointerType) { + return makeNullLiteral(pointerType); + }else{ throw new UnsupportedOperationException("Cannot create zero of type " + type); } } @@ -376,6 +413,8 @@ public Expression makeCast(Expression expression, Type type, boolean signed) { return makeIntegerCast(expression, integerType, signed); } else if (type instanceof FloatType floatType) { return makeFloatCast(expression, floatType, signed); + }else if (type instanceof PointerType) { + return makeIntToPtrCast(makeCast(expression, types.getArchType())); } throw new UnsupportedOperationException(String.format("Cast %s into %s unsupported.", expression, type)); } @@ -394,7 +433,9 @@ public Expression makeEQ(Expression leftOperand, Expression rightOperand) { return makeBoolBinary(leftOperand, BoolBinaryOp.IFF, rightOperand); } else if (type instanceof IntegerType) { return makeIntCmp(leftOperand, IntCmpOp.EQ, rightOperand); - } else if (type instanceof FloatType) { + } if (type instanceof PointerType) { + return makePtrCmp(leftOperand, PointerCmpOp.EQ, rightOperand); + }else if (type instanceof FloatType) { // TODO: Decide on a default semantics for float equality? return makeFloatCmp(leftOperand, FloatCmpOp.OEQ, rightOperand); } else if (ExpressionHelper.isAggregateLike(type)) { @@ -409,7 +450,9 @@ public Expression makeNEQ(Expression leftOperand, Expression rightOperand) { return makeNot(makeBoolBinary(leftOperand, BoolBinaryOp.IFF, rightOperand)); } else if (type instanceof IntegerType) { return makeIntCmp(leftOperand, IntCmpOp.NEQ, rightOperand); - } else if (type instanceof FloatType) { + } if (type instanceof PointerType) { + return makePtrCmp(leftOperand, PointerCmpOp.NEQ, rightOperand); + }else if (type instanceof FloatType) { // TODO: Decide on a default semantics for float equality? return makeFloatCmp(leftOperand, FloatCmpOp.ONEQ, rightOperand); } else if (type instanceof AggregateType) { @@ -438,6 +481,8 @@ public Expression makeBinary(Expression x, ExpressionKind op, Expression y) { return makeFloatBinary(x, floatOp, y); } else if (op instanceof IntCmpOp cmpOp) { return makeCompare(x, cmpOp, y); + }else if (op instanceof PointerCmpOp cmpOp) { + return makeCompare(x, cmpOp, y); } throw new UnsupportedOperationException(String.format("Expression kind %s is no binary operator.", op)); } @@ -449,7 +494,9 @@ public Expression makeCompare(Expression x, ExpressionKind cmpOp, Expression y) return makeFloatCmp(x, floatOp, y); } else if (cmpOp instanceof AggregateCmpOp aggrCmpOp) { return makeAggregateCmp(x, aggrCmpOp, y); + }else if (cmpOp instanceof PointerCmpOp ptrCmpOp) { + return makePtrCmp(x, ptrCmpOp, y); } throw new UnsupportedOperationException(String.format("Expression kind %s is no comparison operator.", cmpOp)); } -} +} \ No newline at end of file diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java index ad586d43ee..e267544d20 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java @@ -7,6 +7,7 @@ import com.dat3m.dartagnan.expression.booleans.BoolUnaryOp; import com.dat3m.dartagnan.expression.floats.FloatSizeCast; import com.dat3m.dartagnan.expression.floats.IntToFloatCast; +import com.dat3m.dartagnan.expression.pointer.*; import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.GEPExpr; import com.dat3m.dartagnan.expression.misc.ITEExpr; @@ -124,6 +125,22 @@ public String visitGEPExpression(GEPExpr expr) { return expr.getOperands().stream().map(this::visit).collect(Collectors.joining(", ", "GEP(", ")")); } + @Override + public String visitIntToPtrCastExpression(IntToPtrCast expr) { + return String.format("%s to %s", visit(expr.getOperand()), expr.getTargetType()); + } + + @Override + public String visitPtrToIntCastExpression(PtrToIntCast expr) { + return String.format("%s to %s", visit(expr.getOperand()), expr.getTargetType()); + } + + @Override + public String visitPointerAddExpression(PointerAddExpr expr) { + return String.format("%s + %s", visit(expr.getBase()), visit(expr.getOffset())); + } + + @Override public String visitITEExpression(ITEExpr expr) { return visit(expr.getCondition()) + " ? " + visit(expr.getTrueCase()) + " : " + visit(expr.getFalseCase()); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/misc/GEPExpr.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/misc/GEPExpr.java index 81891dfa1c..28cb606422 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/misc/GEPExpr.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/misc/GEPExpr.java @@ -6,6 +6,7 @@ import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.base.NaryExpressionBase; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -21,7 +22,7 @@ public final class GEPExpr extends NaryExpressionBase offsets, Integer stride) { super(base.getType(), ExpressionKind.Other.GEP, concat(base, offsets)); - ExpressionHelper.checkExpectedType(base, IntegerType.class); + ExpressionHelper.checkExpectedType(base, PointerType.class); if (offsets.size() > 1) { Preconditions.checkArgument(isAggregateLike(indexType), "Indexing with multiple indices into non-aggregate type."); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java index f2213b26c1..9549e85cbd 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java @@ -17,6 +17,10 @@ import com.dat3m.dartagnan.expression.misc.GEPExpr; import com.dat3m.dartagnan.expression.misc.ITEExpr; import com.dat3m.dartagnan.expression.type.TypeFactory; +import com.dat3m.dartagnan.expression.pointer.IntToPtrCast; +import com.dat3m.dartagnan.expression.pointer.PointerAddExpr; +import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; +import com.dat3m.dartagnan.expression.pointer.PtrToIntCast; import java.util.ArrayList; @@ -117,6 +121,34 @@ public Expression visitAggregateCmpExpression(AggregateCmpExpr expr) { ); } + + @Override + public Expression visitPointerAddExpression(PointerAddExpr expr) { + return expressions.makePtrAdd( + expr.getBase().accept(this), + expr.getOffset().accept(this) + ); + } + + @Override + public Expression visitPtrCmpExpression(PtrCmpExpr expr) { + return expressions.makePtrCmp( + expr.getLeft().accept(this), + expr.getKind(), + expr.getRight().accept(this) + ); + } + + @Override + public Expression visitPtrToIntCastExpression(PtrToIntCast expr) { + return expressions.makePtrToIntCast(expr.getOperand().accept(this)); + } + + @Override + public Expression visitIntToPtrCastExpression(IntToPtrCast expr) { + return expressions.makeIntToPtrCast(expr.getOperand().accept(this)); + } + @Override public Expression visitGEPExpression(GEPExpr gep) { Expression base = gep.getBase().accept(this); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java index df927c90c8..af5f543629 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -3,9 +3,17 @@ import com.dat3m.dartagnan.expression.Type; public class PointerType implements Type { + private int stride; + private Type referredType; - PointerType() { - } + PointerType() {} + + + public int getStride(){ return stride; }; + public void setStride(int s){ stride = s;}; + + public Type getReferredType(){ return referredType; } + public void setReferredType(Type t){ referredType = t;}; @Override public String toString() { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java index 09404a60c8..1c5694fa2e 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java @@ -20,12 +20,13 @@ public final class TypeFactory { private final VoidType voidType = new VoidType(); private final BooleanType booleanType = new BooleanType(); - private final IntegerType pointerDifferenceType; + private final IntegerType archType; + private final PointerType pointerType = new PointerType(); private final Normalizer typeNormalizer = new Normalizer(); private TypeFactory() { - pointerDifferenceType = getIntegerType(64);//TODO insert proper pointer and difference types + archType = getIntegerType(64);//TODO insert proper pointer and difference types } @@ -40,10 +41,11 @@ public BooleanType getBooleanType() { public VoidType getVoidType() { return voidType; } - public Type getPointerType() { - return pointerDifferenceType; + public PointerType getPointerType() { + return pointerType; } + public IntegerType getIntegerType(int bitWidth) { checkArgument(bitWidth > 0, "Non-positive bit width %s.", bitWidth); return typeNormalizer.normalize(new IntegerType(bitWidth)); @@ -141,7 +143,7 @@ public ArrayType getArrayType(Type element, int size, Integer stride, Integer al } public IntegerType getArchType() { - return pointerDifferenceType; + return archType; } public IntegerType getByteType() { @@ -159,6 +161,9 @@ private int getMemorySizeInBytes(Type type, boolean padded) { if (type instanceof IntegerType integerType) { return IntMath.divide(integerType.getBitWidth(), 8, RoundingMode.CEILING); } + if (type instanceof PointerType) { + return getMemorySizeInBytes(getArchType()); + } if (type instanceof FloatType floatType) { return IntMath.divide(floatType.getBitWidth(), 8, RoundingMode.CEILING); } @@ -198,7 +203,7 @@ private int getMemorySizeInBytes(Type type, boolean padded) { } private int getAlignment(Type type) { - if (type instanceof BooleanType || type instanceof IntegerType || type instanceof FloatType) { + if (type instanceof BooleanType || type instanceof IntegerType || type instanceof FloatType || type instanceof PointerType) { return getMemorySizeInBytes(type); } if (type instanceof ArrayType arrayType) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java index 13c5f54df7..372dbe9c16 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java @@ -826,11 +826,12 @@ public Expression visitAddrSpaceCastInst(AddrSpaceCastInstContext ctx) { private Register conversionInstruction(TypeValueContext operand, TypeContext target, boolean signed) { final Expression operandExpression = visitTypeValue(operand); final Type targetType = parseType(target); - checkSupport(targetType instanceof IntegerType, "Non-integer in %s.", target); - final Expression result = expressions.makeIntegerCast(operandExpression, (IntegerType) targetType, signed); + // checkSupport(targetType instanceof IntegerType, "Non-integer in %s.", target); + final Expression result = expressions.makeCast(operandExpression, targetType, signed); return assignToRegister(result); } + // ================================================================================================================= // Expressions @@ -846,7 +847,8 @@ public Expression visitConstant(ConstantContext ctx) { @Override public Expression visitNullConst(NullConstContext ctx) { - return expressions.makeZero((IntegerType) pointerType); + // return expressions.makeZero((IntegerType) pointerType); + return expressions.makeNullLiteral(); } @Override @@ -1068,8 +1070,8 @@ private Expression checkExpression(Type type, ParserRuleContext context) { private Expression castExpression(TypeConstContext operand, TypeContext target, boolean signed) { final Expression operandExpression = visitTypeConst(operand); final Type targetType = parseType(target); - checkSupport(targetType instanceof IntegerType, "Non-integer type %s.", target); - return expressions.makeIntegerCast(operandExpression, (IntegerType) targetType, signed); + // checkSupport(targetType instanceof IntegerType, "Non-integer type %s.", target); + return expressions.makeCast(operandExpression, targetType, signed); } // ---------------------------------------------------------------------------------------------------------------- diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/EventFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/EventFactory.java index 1d9064b9b1..eb66531a0d 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/EventFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/EventFactory.java @@ -165,8 +165,8 @@ public static NamedBarrier newNamedBarrier(String name, String instanceId, Strin public static Init newInit(MemoryObject base, int offset) { //TODO: We simplify here because virtual aliasing currently fails when pointer arithmetic is involved // meaning that and are treated differently. - final Expression address = offset == 0 ? base : - expressions.makeAdd(base, expressions.makeValue(offset, (IntegerType) base.getType())); + // + final Expression address = expressions.makePtrAdd(base, expressions.makeValue(offset, types.getArchType())); final Init init = new Init(base, offset, address); init.addTags(base.getFeatureTags()); return init; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/Memory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/Memory.java index 9c009a2766..45a72f6fed 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/Memory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/Memory.java @@ -4,6 +4,7 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.program.event.core.Alloc; import com.google.common.base.Preconditions; @@ -14,7 +15,7 @@ public class Memory { private final ArrayList objects = new ArrayList<>(); - private final Type ptrType = TypeFactory.getInstance().getPointerType(); + private final PointerType ptrType = TypeFactory.getInstance().getPointerType(); private final IntegerType archType = TypeFactory.getInstance().getArchType(); private final Expression defaultAlignment = ExpressionFactory.getInstance().makeValue(8, archType); private final boolean bigEndian; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/MemoryObject.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/MemoryObject.java index 2f7da6b292..faea593883 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/MemoryObject.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/MemoryObject.java @@ -19,7 +19,7 @@ /** * Associated with an array of memory locations. */ -public class MemoryObject extends LeafExpressionBase { +public class MemoryObject extends LeafExpressionBase { // TODO: (TH) I think is mostly useless. // Its only benefit is that we can have different memory objects with the same name (but why would we?) @@ -34,7 +34,7 @@ public class MemoryObject extends LeafExpressionBase { private final Map initialValues = new TreeMap<>(); - MemoryObject(int id, Expression size, Expression alignment, Alloc allocationSite, Type ptrType) { + MemoryObject(int id, Expression size, Expression alignment, Alloc allocationSite, PointerType ptrType) { super(ptrType); final TypeFactory types = TypeFactory.getInstance(); Preconditions.checkArgument(size.getType() instanceof IntegerType, "Size %s must be of integer type.", size); @@ -123,7 +123,8 @@ public void setInitialValue(int offset, Expression value) { setInitialValue(offset + innerOffset, structElements.get(i)); } } else if (value.getType() instanceof IntegerType - || value.getType() instanceof BooleanType) { + || value.getType() instanceof BooleanType + || value.getType() instanceof PointerType) { checkArgument(isInRange(offset), "array index out of bounds"); initialValues.put(offset, value); } else { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/VirtualMemoryObject.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/VirtualMemoryObject.java index 66b2ea8c7f..f6e0341748 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/VirtualMemoryObject.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/memory/VirtualMemoryObject.java @@ -2,6 +2,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.Type; +import com.dat3m.dartagnan.expression.type.PointerType; import java.util.Objects; @@ -16,7 +17,7 @@ public class VirtualMemoryObject extends MemoryObject { // the generic address of this virtual address private final VirtualMemoryObject genericAddress; - VirtualMemoryObject(int index, Expression size, Expression alignment, boolean generic, VirtualMemoryObject alias, Type ptrType) { + VirtualMemoryObject(int index, Expression size, Expression alignment, boolean generic, VirtualMemoryObject alias, PointerType ptrType) { super(index, size, alignment, null, ptrType); checkArgument(generic || alias != null, "Non-generic virtualMemoryObject must have generic address it alias target"); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/GEPToAddition.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/GEPToAddition.java index 9e7924dfd0..a23dadd066 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/GEPToAddition.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/GEPToAddition.java @@ -97,8 +97,8 @@ public Expression visitGEPExpression(GEPExpr gep) { } final Expression base = gep.getBase().accept(this); - final Expression castOffset = expressions.makeCast(totalOffset, base.getType(), true); - return expressions.makeAdd(base, castOffset); + final Expression castOffset = expressions.makeCast(totalOffset, types.getArchType(), true); + return expressions.makePtrAdd(base, castOffset); } } } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index 284d09bde2..dc24ef2ef7 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -1809,7 +1809,7 @@ private List inlineMemSet(FunctionCall call) { final List replacement = new ArrayList<>( count + 1); for (int i = 0; i < count; i++) { final Expression offset = expressions.makeValue(i, types.getArchType()); - final Expression destAddr = expressions.makeAdd(dest, offset); + final Expression destAddr = expressions.makePtrAdd(dest, offset); replacement.add(newStore(destAddr, zero)); } From ef403bf0523416ae1cb905547204afb2632b18e6 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 15 Sep 2025 15:42:30 +0200 Subject: [PATCH 03/33] ptr correction --- .../com/dat3m/dartagnan/program/processing/Intrinsics.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index dc24ef2ef7..9bd335cddc 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -1609,8 +1609,8 @@ private List inlineMemCpy(FunctionCall call) { final int typeSize = detectMixedSizeAccesses ? count : 1; for (int i = 0; i < count; i += typeSize) { final Expression offset = expressions.makeValue(i, types.getArchType()); - final Expression srcAddr = expressions.makeAdd(src, offset); - final Expression destAddr = expressions.makeAdd(dest, offset); + final Expression srcAddr = expressions.makePtrAdd(src, offset); + final Expression destAddr = expressions.makePtrAdd(dest, offset); final Register reg = caller.getOrNewRegister("__memcpy_" + i, type); final Event load = EventFactory.newLoad(reg, srcAddr); From 2924849192e2b8004458a4566c205184f6aab5b0 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 15 Sep 2025 18:01:20 +0200 Subject: [PATCH 04/33] fixed ptr to int wrong length --- .../dartagnan/encoding/ExpressionEncoder.java | 22 +++++++++++++++---- .../expression/pointer/PtrToIntCast.java | 4 ++-- .../expression/type/PointerType.java | 9 -------- .../program/processing/Intrinsics.java | 1 + 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index bf205c540e..6c3868a119 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -345,7 +345,6 @@ public TypedFormula visitBoolUnaryExpression(BoolUn public TypedFormula visitIntSizeCastExpression(IntSizeCast expr) { final TypedFormula inner = encodeIntegerExpr(expr.getOperand()); final Formula enc; - if (expr.isNoop()) { return inner; } else if (context.useIntegers) { @@ -358,7 +357,6 @@ public TypedFormula visitBoolUnaryExpression(BoolUn } } else { assert inner.formula() instanceof BitvectorFormula; - final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); final BitvectorFormula innerBv = (BitvectorFormula) inner.formula(); final int targetBitWidth = expr.getTargetType().getBitWidth(); @@ -531,13 +529,29 @@ public TypedFormula visitIntCmpExpression(IntCmpExp @Override public TypedFormula visitPtrToIntCastExpression(PtrToIntCast expr) { - final TypedFormula ptr = encodePointerExpr(expr.getOperand()); - return new TypedFormula<>(expr.getType(), ptr.formula()); + // Todo: add support for extension + final TypedFormula inner = encodePointerExpr(expr.getOperand()); + final Formula enc; + if (context.useIntegers) { + final BigInteger highValue = BigInteger.TWO.pow(expr.getType().getBitWidth()); + final IntegerFormulaManager imgr = integerFormulaManager(); + enc = imgr.modulo((IntegerFormula) inner.formula(), imgr.makeNumber(highValue)); + } else { + assert inner.formula() instanceof BitvectorFormula; + final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); + final BitvectorFormula innerBv = (BitvectorFormula) inner.formula(); + final int targetBitWidth = expr.getTargetType().getBitWidth(); + final int sourceBitWidth = types.getArchType().getBitWidth(); + assert (sourceBitWidth == bvmgr.getLength(innerBv)); + enc = bvmgr.extract(innerBv, targetBitWidth - 1, 0); + } + return new TypedFormula<>(expr.getType(), enc); } @Override public TypedFormula visitIntToPtrCastExpression(IntToPtrCast expr) { + final TypedFormula address = encodeIntegerExpr(expr.getOperand()); return new TypedFormula<>(expr.getType(), address.formula()); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java index 97a30b1e59..82a370ef63 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java @@ -9,8 +9,8 @@ public final class PtrToIntCast extends CastExpressionBase { - public PtrToIntCast(IntegerType archType, Expression operand) { - super(archType, operand); + public PtrToIntCast(IntegerType integerType, Expression operand) { + super(integerType, operand); ExpressionHelper.checkExpectedType(operand, PointerType.class); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java index af5f543629..67513c88b0 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -3,18 +3,9 @@ import com.dat3m.dartagnan.expression.Type; public class PointerType implements Type { - private int stride; - private Type referredType; PointerType() {} - - public int getStride(){ return stride; }; - public void setStride(int s){ stride = s;}; - - public Type getReferredType(){ return referredType; } - public void setReferredType(Type t){ referredType = t;}; - @Override public String toString() { return "ptr"; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index 9bd335cddc..f222dc21d2 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -712,6 +712,7 @@ private List inlinePthreadKeyCreate(FunctionCall call) { final Expression destructor = call.getArguments().get(1); final Program program = call.getFunction().getProgram(); final long threadCount = program.getThreads().size(); + final int pointerBytes = types.getMemorySizeInBytes(types.getPointerType()); final Register storageAddressRegister = call.getFunction().newRegister(types.getArchType()); final Expression size = expressions.makeValue((threadCount + 1) * pointerBytes, types.getArchType()); From 8515febe3e23620d47085cef43ba215b66b22308 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Fri, 19 Sep 2025 13:34:56 +0200 Subject: [PATCH 05/33] more fixes related to arch and ptr --- .../dat3m/dartagnan/expression/type/TypeFactory.java | 3 +-- .../parsers/program/utils/ProgramBuilder.java | 4 ++-- .../program/visitors/VisitorLitmusAArch64.java | 2 +- .../parsers/program/visitors/VisitorLitmusC.java | 11 ++++++++--- .../parsers/program/visitors/VisitorLitmusPPC.java | 2 +- .../parsers/program/visitors/VisitorLitmusPTX.java | 3 +-- .../parsers/program/visitors/VisitorLitmusRISCV.java | 2 +- .../parsers/program/visitors/VisitorLitmusVulkan.java | 2 +- .../parsers/program/visitors/VisitorLitmusX86.java | 2 +- 9 files changed, 17 insertions(+), 14 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java index 1c5694fa2e..905ddb2168 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java @@ -22,11 +22,10 @@ public final class TypeFactory { private final BooleanType booleanType = new BooleanType(); private final IntegerType archType; private final PointerType pointerType = new PointerType(); - private final Normalizer typeNormalizer = new Normalizer(); private TypeFactory() { - archType = getIntegerType(64);//TODO insert proper pointer and difference types + archType = getIntegerType(64); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/utils/ProgramBuilder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/utils/ProgramBuilder.java index 880eb08671..292dbd995c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/utils/ProgramBuilder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/utils/ProgramBuilder.java @@ -242,9 +242,9 @@ public void initLocEqConst(String locName, Expression iValue){ getOrNewMemoryObject(locName).setInitialValue(0, iValue); } - public void initRegEqLocPtr(int regThread, String regName, String locName, Type type) { + public void initRegEqLocPtr(int regThread, String regName, String locName) { MemoryObject object = getOrNewMemoryObject(locName); - Register reg = getOrNewRegister(regThread, regName, type); + Register reg = getOrNewRegister(regThread, regName, types.getPointerType()); addChild(regThread, EventFactory.newLocal(reg, object)); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java index 3f743fee51..820f69b129 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java @@ -108,7 +108,7 @@ public Object visitTypedRegisterDeclarator(TypedRegisterDeclaratorContext ctx) { @Override public Object visitVariableDeclaratorRegisterLocation(VariableDeclaratorRegisterLocationContext ctx) { - programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register64().id, ctx.location().getText(), i64); + programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register64().id, ctx.location().getText()); return null; } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index 1b9df82763..be67a46f6c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -6,6 +6,7 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.integers.IntLiteral; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.parsers.LitmusCBaseVisitor; import com.dat3m.dartagnan.parsers.LitmusCParser; @@ -29,6 +30,7 @@ public class VisitorLitmusC extends LitmusCBaseVisitor { private final ProgramBuilder programBuilder = ProgramBuilder.forLanguage(Program.SourceLanguage.LITMUS); private final ExpressionFactory expressions = programBuilder.getExpressionFactory(); private final IntegerType archType = programBuilder.getTypeFactory().getArchType(); + private final PointerType pointerType = programBuilder.getTypeFactory().getPointerType(); private final int archSize = TypeFactory.getInstance().getMemorySizeInBytes(archType); private int currentThread; private int scope; @@ -102,7 +104,7 @@ public Object visitGlobalDeclaratorLocationLocation(LitmusCParser.GlobalDeclarat public Object visitGlobalDeclaratorRegisterLocation(LitmusCParser.GlobalDeclaratorRegisterLocationContext ctx) { // FIXME: We visit declarators before threads, so we need to create threads early if(ctx.Ast() == null){ - programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.varName(0).getText(), ctx.varName(1).getText(), archType); + programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.varName(0).getText(), ctx.varName(1).getText()); } else { String rightName = ctx.varName(1).getText(); MemoryObject object = programBuilder.getMemoryObject(rightName); @@ -196,7 +198,7 @@ public Object visitThreadArgument(LitmusCParser.ThreadArgumentContext ctx) { // For now, herd7 also seems to ignore most modifiers, in particular the atomic one. String name = ctx.varName().getText(); MemoryObject object = programBuilder.getOrNewMemoryObject(name); - Register register = programBuilder.getOrNewRegister(scope, name, archType); + Register register = programBuilder.getOrNewRegister(scope, name, pointerType); boolean atomicity = ctx.pointerTypeSpecifier().atomicTypeSpecifier() != null || ctx.pointerTypeSpecifier().basicTypeSpecifier().AtomicInt() != null; if (!atomicity) { @@ -689,7 +691,10 @@ public Expression visitVarName(LitmusCParser.VarNameContext ctx){ private Expression getAddress(LitmusCParser.ReContext ctx){ Expression address = (Expression)ctx.accept(this); if(address.getType() instanceof IntegerType){ - return address; + return address; //todo: remove + } + if(address.getType() instanceof PointerType){ + return address; } throw new ParsingException("Invalid syntax near " + ctx.getText()); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPPC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPPC.java index 2fe6be4258..6dc700bb5e 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPPC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPPC.java @@ -70,7 +70,7 @@ public Object visitVariableDeclaratorRegister(LitmusPPCParser.VariableDeclarator @Override public Object visitVariableDeclaratorRegisterLocation(LitmusPPCParser.VariableDeclaratorRegisterLocationContext ctx) { - programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText(), archType); + programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText()); return null; } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPTX.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPTX.java index 2f7da8d178..806ccde72c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPTX.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPTX.java @@ -67,8 +67,7 @@ public Object visitVariableDeclaratorRegister(LitmusPTXParser.VariableDeclarator @Override public Object visitVariableDeclaratorRegisterLocation(LitmusPTXParser.VariableDeclaratorRegisterLocationContext ctx) { - programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText(), - archType); + programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText()); return null; } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusRISCV.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusRISCV.java index 92568decfd..7fc60ae5a8 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusRISCV.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusRISCV.java @@ -65,7 +65,7 @@ public Object visitVariableDeclaratorRegister(LitmusRISCVParser.VariableDeclarat @Override public Object visitVariableDeclaratorRegisterLocation(LitmusRISCVParser.VariableDeclaratorRegisterLocationContext ctx) { - programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText(), archType); + programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText()); return null; } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusVulkan.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusVulkan.java index b26aa5bdd9..a7019f2c82 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusVulkan.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusVulkan.java @@ -69,7 +69,7 @@ public Object visitVariableDeclaratorRegister(LitmusVulkanParser.VariableDeclara @Override public Object visitVariableDeclaratorRegisterLocation(LitmusVulkanParser.VariableDeclaratorRegisterLocationContext ctx) { - programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText(), archType); + programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText()); return null; } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusX86.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusX86.java index dfd674dbfb..f84d804ddf 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusX86.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusX86.java @@ -63,7 +63,7 @@ public Object visitVariableDeclaratorRegister(LitmusX86Parser.VariableDeclarator @Override public Object visitVariableDeclaratorRegisterLocation(LitmusX86Parser.VariableDeclaratorRegisterLocationContext ctx) { - programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText(), archType); + programBuilder.initRegEqLocPtr(ctx.threadId().id, ctx.register().getText(), ctx.location().getText()); return null; } From 64d1750b7a9fceb94fe82bafcabe319d57ef0a7f Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Fri, 19 Sep 2025 16:47:04 +0200 Subject: [PATCH 06/33] more fixes and added PtrLiteral --- .../dartagnan/encoding/ExpressionEncoder.java | 7 ++++ .../expression/ExpressionFactory.java | 5 +++ .../expression/ExpressionVisitor.java | 2 +- .../expression/pointer/PointerLiteral.java | 39 +++++++++++++++++++ .../program/processing/MemToReg.java | 3 +- .../processing/NaiveDevirtualisation.java | 17 ++++---- .../program/processing/ThreadCreation.java | 15 ++++--- 7 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerLiteral.java diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index 6c3868a119..43630a589a 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -507,6 +507,13 @@ public TypedFormula visitIntCmpExpression(IntCmpExp // ==================================================================================== // Pointers + @Override + public TypedFormula visitPtrLiteral(PointerLiteral literal) { + final Formula result = context.useIntegers + ? integerFormulaManager().makeNumber(literal.getValue()) + : bitvectorFormulaManager().makeBitvector(types.getArchType().getBitWidth(), literal.getValue()); + return new TypedFormula<>(literal.getType(), result); + } @Override public TypedFormula visitPointerAddExpression(PointerAddExpr expr) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index c40699e884..d3752c082d 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -317,6 +317,10 @@ public Expression makeAggregateCmp(Expression x, AggregateCmpOp op, Expression y // ----------------------------------------------------------------------------------------------------------------- // Pointers + public PointerLiteral makeValue(BigInteger value, PointerType type) { + return new PointerLiteral(type, value); + } + public Expression makeGetElementPointer(Type indexingType, Expression base, List offsets) { Preconditions.checkArgument(base.getType().equals(types.getPointerType()), "Applying offsets to non-pointer expression."); @@ -360,6 +364,7 @@ public Expression makeIntToPtrCast(Expression operand) { return new IntToPtrCast(types.getPointerType(), operand); } + public Expression makeNullLiteral(PointerType pointerType) { return new NullLiteral(pointerType); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index cd7c728fc0..2d40c695dd 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -63,6 +63,7 @@ public interface ExpressionVisitor { default TRet visitPtrToIntCastExpression(PtrToIntCast expr) { return visitCastExpression(expr); } default TRet visitPtrCmpExpression(PtrCmpExpr expr) { return visitBinaryExpression(expr); } default TRet visitNullLiteral(NullLiteral lit) { return visitLeafExpression(lit); } + default TRet visitPtrLiteral(PointerLiteral lit) {return visitLeafExpression(lit);} // =================================== Generic =================================== default TRet visitITEExpression(ITEExpr expr) { return visitExpression(expr); } @@ -80,5 +81,4 @@ private static UnsupportedOperationException unsupported(Expression expr, Class< expr.getClass().getSimpleName(), clazz.getSimpleName()); return new UnsupportedOperationException(error); } - } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerLiteral.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerLiteral.java new file mode 100644 index 0000000000..e668e72c0e --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerLiteral.java @@ -0,0 +1,39 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.LiteralExpressionBase; +import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; +import com.google.common.base.Preconditions; + +import java.math.BigInteger; + +public final class PointerLiteral extends LiteralExpressionBase { + + private final BigInteger value; + + public PointerLiteral(PointerType type, BigInteger value) { + super(type); + this.value = value; + } + + public BigInteger getValue() { + return value; + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitPtrLiteral(this); + } + + + @Override + public boolean equals(Object o) { + return this == o || o instanceof PointerLiteral val && getType().equals(val.getType()) && value.equals(val.value); + } + + @Override + public String toString() { + return String.format("%s(%s)", getType(), value); + } +} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/MemToReg.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/MemToReg.java index 4703832749..8a54156b58 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/MemToReg.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/MemToReg.java @@ -42,8 +42,7 @@ public class MemToReg implements FunctionProcessor { private static final TypeFactory types = TypeFactory.getInstance(); private static final ExpressionFactory expressions = ExpressionFactory.getInstance(); - private MemToReg() { - } + private MemToReg() {} public static MemToReg fromConfig(Configuration config) throws InvalidConfigurationException { return new MemToReg(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java index 44c462552f..7b91819b7c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java @@ -4,9 +4,11 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.integers.IntLiteral; +import com.dat3m.dartagnan.expression.pointer.PointerLiteral; import com.dat3m.dartagnan.expression.processing.ExprTransformer; import com.dat3m.dartagnan.expression.processing.ExpressionInspector; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.IRHelper; @@ -24,6 +26,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; @@ -97,12 +100,12 @@ private void findAndTransformAddressTakenFunctions( } } - private boolean assignAddressToFunction(Function func, Map func2AddressMap) { - final IntegerType ptrType = TypeFactory.getInstance().getArchType(); + private boolean assignAddressToFunction(Function func, Map func2AddressMap) { + final PointerType ptrType = TypeFactory.getInstance().getPointerType(); final ExpressionFactory expressions = ExpressionFactory.getInstance(); if (!func2AddressMap.containsKey(func)) { logger.debug("Assigned address \"{}\" to function \"{}\"", nextAvailableFuncAddress, func); - func2AddressMap.put(func, expressions.makeValue(nextAvailableFuncAddress, ptrType)); + func2AddressMap.put(func, expressions.makeValue(BigInteger.valueOf(nextAvailableFuncAddress), ptrType)); nextAvailableFuncAddress += 8; return true; } @@ -119,7 +122,7 @@ private void applyTransformerToEvent(Event e, ExpressionVisitor tran } } - private void devirtualise(Function function, Map func2AddressMap) { + private void devirtualise(Function function, Map func2AddressMap) { final ExpressionFactory expressions = ExpressionFactory.getInstance(); int devirtCounter = 0; @@ -147,7 +150,7 @@ private void devirtualise(Function function, Map func2Addr final Expression funcPtr = call.getCallTarget(); // Construct call table for (Function possibleTarget : possibleTargets) { - final IntLiteral targetAddress = func2AddressMap.get(possibleTarget); + final PointerLiteral targetAddress = func2AddressMap.get(possibleTarget); final Label caseLabel = EventFactory.newLabel(String.format("__Ldevirt_%s#%s", targetAddress.getValue(), devirtCounter)); final CondJump caseJump = EventFactory.newJump(expressions.makeEQ(funcPtr, targetAddress), caseLabel); caseLabels.add(caseLabel); @@ -178,7 +181,7 @@ private boolean needsDevirtualization(CallEvent call) { return !call.isDirectCall(); } - private List getPossibleTargets(CallEvent call, Map func2AddressMap) { + private List getPossibleTargets(CallEvent call, Map func2AddressMap) { Preconditions.checkArgument(needsDevirtualization(call)); return func2AddressMap.keySet().stream() .filter(f -> f.getFunctionType() == call.getCallType()) @@ -210,7 +213,7 @@ public Expression visitFunction(Function function) { private static class FunctionToAddressTransformer extends ExprTransformer { - private final Map func2AddressMap = new HashMap<>(); + private final Map func2AddressMap = new HashMap<>(); @Override public Expression visitFunction(Function function) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java index 8ba1af9081..a6585245b2 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java @@ -7,10 +7,7 @@ import com.dat3m.dartagnan.expression.integers.IntBinaryOp; import com.dat3m.dartagnan.expression.integers.IntLiteral; import com.dat3m.dartagnan.expression.processing.ExprTransformer; -import com.dat3m.dartagnan.expression.type.AggregateType; -import com.dat3m.dartagnan.expression.type.FunctionType; -import com.dat3m.dartagnan.expression.type.IntegerType; -import com.dat3m.dartagnan.expression.type.TypeFactory; +import com.dat3m.dartagnan.expression.type.*; import com.dat3m.dartagnan.program.*; import com.dat3m.dartagnan.program.Thread; import com.dat3m.dartagnan.program.event.*; @@ -85,6 +82,7 @@ public class ThreadCreation implements ProgramProcessor { private final IntegerType archType = types.getArchType(); // The thread state consists of two flags: ALIVE and JOINABLE. private final IntegerType threadStateType = types.getIntegerType(2); + private final PointerType pointerType = types.getPointerType(); private ThreadCreation(Configuration config) throws InvalidConfigurationException { config.inject(this); @@ -186,7 +184,12 @@ private void resolveDynamicThreadJoin(Program program, List threadDa for (DynamicThreadJoin join : program.getThreadEvents(DynamicThreadJoin.class)) { final Thread caller = join.getThread(); - final Expression tidExpr = join.getTid(); + final Expression tidExpr_ = join.getTid(); + final Expression tidExpr = tidExpr_.getType() instanceof PointerType ? expressions.makePtrToIntCast(tidExpr_) : tidExpr_; + + + + final Register joinRegister = join.getResultRegister(); final IntegerType statusType = (IntegerType) ((AggregateType)joinRegister.getType()).getFields().get(0).type(); @@ -462,7 +465,7 @@ private void replaceThreadLocalsWithStackalloc(Memory memory, Thread thread) { final List initialization = new ArrayList<>(); for (Integer initOffset : memoryObject.getInitializedFields()) { initialization.add(EventFactory.newStore( - exprs.makeAdd(reg, exprs.makeValue(initOffset, types.getArchType())), + exprs.makePtrAdd(reg, exprs.makeValue(initOffset, types.getArchType())), memoryObject.getInitialValue(initOffset) )); } From b79736b98879f0fcf084669cee6c2364f4e8f92b Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Tue, 23 Sep 2025 15:53:09 +0200 Subject: [PATCH 07/33] visitor changes --- .../dartagnan/parsers/program/visitors/VisitorAsmArm.java | 8 +++++--- .../parsers/program/visitors/VisitorLitmusAArch64.java | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java index f628fa6f45..ae56ca6b2b 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java @@ -10,7 +10,9 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.integers.IntCmpOp; +import com.dat3m.dartagnan.expression.pointer.PointerCmpOp; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.parsers.AsmArmBaseVisitor; import com.dat3m.dartagnan.parsers.AsmArmParser; import com.dat3m.dartagnan.parsers.program.utils.AsmUtils; @@ -300,7 +302,7 @@ public Object visitBranchEqual(AsmArmParser.BranchEqualContext ctx) { @Override public Object visitBranchNotEqual(AsmArmParser.BranchNotEqualContext ctx) { Label label = AsmUtils.getOrNewLabel(labelsDefined, ctx.Numbers().getText()); - Expression expr = expressions.makeIntCmp(comparator.left(), IntCmpOp.NEQ, comparator.right()); + Expression expr = expressions.makePtrCmp(comparator.left(), PointerCmpOp.NEQ, comparator.right()); asmInstructions.add(EventFactory.newJump(expr, label)); return null; } @@ -388,10 +390,10 @@ public Object visitAsmMetadataEntries(AsmArmParser.AsmMetadataEntriesContext ctx @Override public Object visitValue(AsmArmParser.ValueContext ctx) { - checkState(expectedType instanceof IntegerType, "Expected type is not an integer type"); + checkState(expectedType instanceof IntegerType || expectedType instanceof PointerType, "Expected type is not an integer type"); String valueString = ctx.Numbers().getText(); BigInteger value = new BigInteger(valueString); - return expressions.makeValue(value, (IntegerType) expectedType); + return expectedType instanceof IntegerType ? expressions.makeValue(value, (IntegerType) expectedType):expressions.makeValue(value, (PointerType) expectedType); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java index 820f69b129..9e6174780b 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java @@ -193,7 +193,7 @@ public Object visitLoadPair(LoadPairContext ctx) { final Register value0 = extended ? r064 : shrinkRegister(r064, ctx.rD032, false, false); final Register value1 = extended ? r164 : shrinkRegister(r164, ctx.rD132, false, false); final Expression address0 = parseAddress(ctx.address()); - final Expression address1 = expressions.makeAdd(address0, expressions.makeValue(extended ? 8 : 4, i64)); + final Expression address1 = expressions.makePtrAdd(address0, expressions.makeValue(extended ? 8 : 4, i64)); add(EventFactory.newLoad(value0, address0)); add(EventFactory.newLoad(value1, address1)); addRegister64Update(r064, value0); @@ -233,7 +233,7 @@ public Object visitStorePair(StorePairContext ctx) { final Expression value0 = expressions.makeIntegerCast(r64, type, false); final Expression value1 = expressions.makeIntegerCast(s64, type, false); final Expression address0 = parseAddress(ctx.address()); - final Expression address1 = expressions.makeAdd(address0, expressions.makeValue(extended ? 8 : 4, i64)); + final Expression address1 = expressions.makePtrAdd(address0, expressions.makeValue(extended ? 8 : 4, i64)); add(EventFactory.newStore(address0, value0)); return add(EventFactory.newStore(address1, value1)); } @@ -394,7 +394,7 @@ private Expression parseAddress(AddressContext ctx) { final Register64Context register64 = ctx.offset().register64(); final ImmediateContext imm = ctx.offset().immediate(); final Expression offset = imm == null ? parseRegister64(register32, register64) : parseValue(imm.constant(), i64); - return expressions.makeAdd(base, offset); + return expressions.makePtrAdd(base, offset); } private Register parseRegister64(Register32Context w) { From 692b89353a8ca1d3c624a587c087176447ec18ab Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 20 Oct 2025 09:45:39 +0200 Subject: [PATCH 08/33] removed intLiteral --- .../dartagnan/encoding/ExpressionEncoder.java | 7 ------ .../expression/ExpressionFactory.java | 16 ++++++++++---- .../expression/ExpressionVisitor.java | 1 - .../program/visitors/VisitorAsmArm.java | 4 ++-- .../parsers/program/visitors/VisitorLlvm.java | 2 +- .../event/core/AbstractMemoryCoreEvent.java | 2 ++ .../program/processing/Intrinsics.java | 12 +++++----- .../processing/NaiveDevirtualisation.java | 18 +++++++-------- .../program/processing/ThreadCreation.java | 22 ++++++++++--------- 9 files changed, 43 insertions(+), 41 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index 43630a589a..6c3868a119 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -507,13 +507,6 @@ public TypedFormula visitIntCmpExpression(IntCmpExp // ==================================================================================== // Pointers - @Override - public TypedFormula visitPtrLiteral(PointerLiteral literal) { - final Formula result = context.useIntegers - ? integerFormulaManager().makeNumber(literal.getValue()) - : bitvectorFormulaManager().makeBitvector(types.getArchType().getBitWidth(), literal.getValue()); - return new TypedFormula<>(literal.getType(), result); - } @Override public TypedFormula visitPointerAddExpression(PointerAddExpr expr) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index d3752c082d..6a1e85d8eb 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -106,18 +106,30 @@ public IntLiteral makeValue(BigInteger value, IntegerType type) { } public Expression makeLT(Expression leftOperand, Expression rightOperand, boolean signed) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.LT : IntCmpOp.ULT, makePtrToIntCast(rightOperand)); + } return makeIntCmp(leftOperand, signed ? IntCmpOp.LT : IntCmpOp.ULT, rightOperand); } public Expression makeGT(Expression leftOperand, Expression rightOperand, boolean signed) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.GT : IntCmpOp.UGT, makePtrToIntCast(rightOperand)); + } return makeIntCmp(leftOperand, signed ? IntCmpOp.GT : IntCmpOp.UGT, rightOperand); } public Expression makeLTE(Expression leftOperand, Expression rightOperand, boolean signed) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.LTE : IntCmpOp.ULTE, makePtrToIntCast(rightOperand)); + } return makeIntCmp(leftOperand, signed ? IntCmpOp.LTE : IntCmpOp.ULTE, rightOperand); } public Expression makeGTE(Expression leftOperand, Expression rightOperand, boolean signed) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.GTE : IntCmpOp.UGTE, makePtrToIntCast(rightOperand)); + } return makeIntCmp(leftOperand, signed ? IntCmpOp.GTE : IntCmpOp.UGTE, rightOperand); } @@ -317,10 +329,6 @@ public Expression makeAggregateCmp(Expression x, AggregateCmpOp op, Expression y // ----------------------------------------------------------------------------------------------------------------- // Pointers - public PointerLiteral makeValue(BigInteger value, PointerType type) { - return new PointerLiteral(type, value); - } - public Expression makeGetElementPointer(Type indexingType, Expression base, List offsets) { Preconditions.checkArgument(base.getType().equals(types.getPointerType()), "Applying offsets to non-pointer expression."); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index 2d40c695dd..9023cb0310 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -63,7 +63,6 @@ public interface ExpressionVisitor { default TRet visitPtrToIntCastExpression(PtrToIntCast expr) { return visitCastExpression(expr); } default TRet visitPtrCmpExpression(PtrCmpExpr expr) { return visitBinaryExpression(expr); } default TRet visitNullLiteral(NullLiteral lit) { return visitLeafExpression(lit); } - default TRet visitPtrLiteral(PointerLiteral lit) {return visitLeafExpression(lit);} // =================================== Generic =================================== default TRet visitITEExpression(ITEExpr expr) { return visitExpression(expr); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java index ae56ca6b2b..452bae2f16 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java @@ -390,10 +390,10 @@ public Object visitAsmMetadataEntries(AsmArmParser.AsmMetadataEntriesContext ctx @Override public Object visitValue(AsmArmParser.ValueContext ctx) { - checkState(expectedType instanceof IntegerType || expectedType instanceof PointerType, "Expected type is not an integer type"); + checkState(expectedType instanceof IntegerType, "Expected type is not an integer type"); String valueString = ctx.Numbers().getText(); BigInteger value = new BigInteger(valueString); - return expectedType instanceof IntegerType ? expressions.makeValue(value, (IntegerType) expectedType):expressions.makeValue(value, (PointerType) expectedType); + return expressions.makeValue(value, (IntegerType) expectedType); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java index 372dbe9c16..b69facef3c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java @@ -50,7 +50,7 @@ public class VisitorLlvm extends LLVMIRBaseVisitor { private final Program program; private final TypeFactory types = TypeFactory.getInstance(); private final ExpressionFactory expressions = ExpressionFactory.getInstance(); - private final Type pointerType = types.getPointerType(); + private final PointerType pointerType = types.getPointerType(); private final IntegerType integerType = types.getArchType(); private final Map constantMap = new HashMap<>(); private final Map typeDefinitionMap = new HashMap<>(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java index a9eeb7a068..b8edaddb13 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java @@ -3,6 +3,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.Type; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.program.event.AbstractEvent; import static com.dat3m.dartagnan.program.event.Tag.MEMORY; import static com.dat3m.dartagnan.program.event.Tag.VISIBLE; @@ -19,6 +20,7 @@ public abstract class AbstractMemoryCoreEvent extends AbstractEvent implements M protected Type accessType; public AbstractMemoryCoreEvent(Expression address, Type accessType) { + Preconditions.checkArgument(address.getType() instanceof PointerType); this.address = Preconditions.checkNotNull(address); this.accessType = accessType; addTags(VISIBLE, MEMORY); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index f222dc21d2..93f4912733 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -1654,7 +1654,7 @@ private List inlineMemCpyS(FunctionCall call) { final int destsz = destszValue.getValueAsInt(); // Runtime checks - final Expression nullExpr = expressions.makeZero(types.getArchType()); + final Expression nullExpr = expressions.makeNullLiteral(); final Expression destIsNull = expressions.makeEQ(dest, nullExpr); final Expression srcIsNull = expressions.makeEQ(src, nullExpr); @@ -1669,8 +1669,8 @@ private List inlineMemCpyS(FunctionCall call) { final Expression countGtdestszExpr = expressions.makeGT(castCountExpr, castDestszExpr, false); final Expression invalidCount = expressions.makeOr(countGtMax, countGtdestszExpr); final Expression overlap = expressions.makeAnd( - expressions.makeGT(expressions.makeAdd(src, castCountExpr), dest, false), - expressions.makeGT(expressions.makeAdd(dest, castCountExpr), src, false)); + expressions.makeGT(expressions.makePtrToIntCast(expressions.makePtrAdd(src, castCountExpr)), expressions.makePtrToIntCast(dest), false), + expressions.makeGT(expressions.makePtrToIntCast(expressions.makePtrAdd(dest, castCountExpr)), expressions.makePtrToIntCast(src), false)); final List replacement = new ArrayList<>(); @@ -1707,7 +1707,7 @@ private List inlineMemCpyS(FunctionCall call) { )); for (int i = 0; i < destsz; i++) { final Expression offset = expressions.makeValue(i, types.getArchType()); - final Expression destAddr = expressions.makeAdd(dest, offset); + final Expression destAddr = expressions.makePtrAdd(dest, offset); final Expression zero = expressions.makeZero(types.getArchType()); replacement.add( newStore(destAddr, zero) @@ -1723,8 +1723,8 @@ private List inlineMemCpyS(FunctionCall call) { replacement.add(success); for (int i = 0; i < count; i++) { final Expression offset = expressions.makeValue(i, types.getArchType()); - final Expression srcAddr = expressions.makeAdd(src, offset); - final Expression destAddr = expressions.makeAdd(dest, offset); + final Expression srcAddr = expressions.makePtrAdd(src, offset); + final Expression destAddr = expressions.makePtrAdd(dest, offset); // FIXME: We have no other choice but to load ptr-sized chunks for now final Register reg = caller.getOrNewRegister("__memcpy_" + i, types.getArchType()); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java index 7b91819b7c..fcd8fc4e74 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java @@ -4,11 +4,9 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.integers.IntLiteral; -import com.dat3m.dartagnan.expression.pointer.PointerLiteral; import com.dat3m.dartagnan.expression.processing.ExprTransformer; import com.dat3m.dartagnan.expression.processing.ExpressionInspector; import com.dat3m.dartagnan.expression.type.IntegerType; -import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.IRHelper; @@ -100,12 +98,12 @@ private void findAndTransformAddressTakenFunctions( } } - private boolean assignAddressToFunction(Function func, Map func2AddressMap) { - final PointerType ptrType = TypeFactory.getInstance().getPointerType(); + private boolean assignAddressToFunction(Function func, Map func2AddressMap) { + final IntegerType ptrType = TypeFactory.getInstance().getArchType(); final ExpressionFactory expressions = ExpressionFactory.getInstance(); if (!func2AddressMap.containsKey(func)) { logger.debug("Assigned address \"{}\" to function \"{}\"", nextAvailableFuncAddress, func); - func2AddressMap.put(func, expressions.makeValue(BigInteger.valueOf(nextAvailableFuncAddress), ptrType)); + func2AddressMap.put(func, expressions.makeValue(BigInteger.valueOf(nextAvailableFuncAddress),ptrType)); nextAvailableFuncAddress += 8; return true; } @@ -122,7 +120,7 @@ private void applyTransformerToEvent(Event e, ExpressionVisitor tran } } - private void devirtualise(Function function, Map func2AddressMap) { + private void devirtualise(Function function, Map func2AddressMap) { final ExpressionFactory expressions = ExpressionFactory.getInstance(); int devirtCounter = 0; @@ -150,9 +148,9 @@ private void devirtualise(Function function, Map func2 final Expression funcPtr = call.getCallTarget(); // Construct call table for (Function possibleTarget : possibleTargets) { - final PointerLiteral targetAddress = func2AddressMap.get(possibleTarget); + final IntLiteral targetAddress = func2AddressMap.get(possibleTarget); final Label caseLabel = EventFactory.newLabel(String.format("__Ldevirt_%s#%s", targetAddress.getValue(), devirtCounter)); - final CondJump caseJump = EventFactory.newJump(expressions.makeEQ(funcPtr, targetAddress), caseLabel); + final CondJump caseJump = EventFactory.newJump(expressions.makeEQ(expressions.makePtrToIntCast(funcPtr), targetAddress), caseLabel); caseLabels.add(caseLabel); caseJumps.add(caseJump); } @@ -181,7 +179,7 @@ private boolean needsDevirtualization(CallEvent call) { return !call.isDirectCall(); } - private List getPossibleTargets(CallEvent call, Map func2AddressMap) { + private List getPossibleTargets(CallEvent call, Map func2AddressMap) { Preconditions.checkArgument(needsDevirtualization(call)); return func2AddressMap.keySet().stream() .filter(f -> f.getFunctionType() == call.getCallType()) @@ -213,7 +211,7 @@ public Expression visitFunction(Function function) { private static class FunctionToAddressTransformer extends ExprTransformer { - private final Map func2AddressMap = new HashMap<>(); + private final Map func2AddressMap = new HashMap<>(); @Override public Expression visitFunction(Function function) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java index a6585245b2..253db9bcd6 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java @@ -187,10 +187,6 @@ private void resolveDynamicThreadJoin(Program program, List threadDa final Expression tidExpr_ = join.getTid(); final Expression tidExpr = tidExpr_.getType() instanceof PointerType ? expressions.makePtrToIntCast(tidExpr_) : tidExpr_; - - - - final Register joinRegister = join.getResultRegister(); final IntegerType statusType = (IntegerType) ((AggregateType)joinRegister.getType()).getFields().get(0).type(); final Type retValType = ((AggregateType)joinRegister.getType()).getFields().get(1).type(); @@ -342,11 +338,18 @@ private void resolvePthreadSelf(Program program) { } if (call.getCalledFunction().getIntrinsicInfo() == Intrinsics.Info.P_THREAD_SELF) { final Register resultRegister = getResultRegister(call); - assert resultRegister.getType() instanceof IntegerType; + Type regType = resultRegister.getType(); + assert regType instanceof PointerType || regType instanceof IntegerType; assert call.getArguments().isEmpty(); - final Expression tidExpr = new TIdExpr((IntegerType) resultRegister.getType(), call.getThread()); - final Local tidAssignment = newLocal(resultRegister, tidExpr); - IRHelper.replaceWithMetadata(call, tidAssignment); + if (regType instanceof PointerType) { + final Expression tidExpr = new TIdExpr(archType, call.getThread()); + final Local tidAssignment = newLocal(resultRegister, expressions.makeIntToPtrCast(tidExpr)); + IRHelper.replaceWithMetadata(call, tidAssignment); + }else { + final Expression tidExpr = new TIdExpr((IntegerType) regType, call.getThread()); + final Local tidAssignment = newLocal(resultRegister, tidExpr); + IRHelper.replaceWithMetadata(call, tidAssignment); + } } } } @@ -660,5 +663,4 @@ public T accept(ExpressionVisitor visitor) { } } - -} +} \ No newline at end of file From 2321bd808c11ae32bcdca8f95d7bb6fcb656995e Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 20 Oct 2025 09:45:39 +0200 Subject: [PATCH 09/33] removed PtrLiteral --- .../dartagnan/encoding/ExpressionEncoder.java | 7 ---- .../expression/ExpressionFactory.java | 16 ++++++-- .../expression/ExpressionVisitor.java | 1 - .../expression/pointer/PointerLiteral.java | 39 ------------------- .../program/visitors/VisitorAsmArm.java | 4 +- .../parsers/program/visitors/VisitorLlvm.java | 2 +- .../event/core/AbstractMemoryCoreEvent.java | 2 + .../program/processing/Intrinsics.java | 12 +++--- .../processing/NaiveDevirtualisation.java | 18 ++++----- .../program/processing/ThreadCreation.java | 22 ++++++----- 10 files changed, 43 insertions(+), 80 deletions(-) delete mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerLiteral.java diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index 43630a589a..6c3868a119 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -507,13 +507,6 @@ public TypedFormula visitIntCmpExpression(IntCmpExp // ==================================================================================== // Pointers - @Override - public TypedFormula visitPtrLiteral(PointerLiteral literal) { - final Formula result = context.useIntegers - ? integerFormulaManager().makeNumber(literal.getValue()) - : bitvectorFormulaManager().makeBitvector(types.getArchType().getBitWidth(), literal.getValue()); - return new TypedFormula<>(literal.getType(), result); - } @Override public TypedFormula visitPointerAddExpression(PointerAddExpr expr) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index d3752c082d..6a1e85d8eb 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -106,18 +106,30 @@ public IntLiteral makeValue(BigInteger value, IntegerType type) { } public Expression makeLT(Expression leftOperand, Expression rightOperand, boolean signed) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.LT : IntCmpOp.ULT, makePtrToIntCast(rightOperand)); + } return makeIntCmp(leftOperand, signed ? IntCmpOp.LT : IntCmpOp.ULT, rightOperand); } public Expression makeGT(Expression leftOperand, Expression rightOperand, boolean signed) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.GT : IntCmpOp.UGT, makePtrToIntCast(rightOperand)); + } return makeIntCmp(leftOperand, signed ? IntCmpOp.GT : IntCmpOp.UGT, rightOperand); } public Expression makeLTE(Expression leftOperand, Expression rightOperand, boolean signed) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.LTE : IntCmpOp.ULTE, makePtrToIntCast(rightOperand)); + } return makeIntCmp(leftOperand, signed ? IntCmpOp.LTE : IntCmpOp.ULTE, rightOperand); } public Expression makeGTE(Expression leftOperand, Expression rightOperand, boolean signed) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.GTE : IntCmpOp.UGTE, makePtrToIntCast(rightOperand)); + } return makeIntCmp(leftOperand, signed ? IntCmpOp.GTE : IntCmpOp.UGTE, rightOperand); } @@ -317,10 +329,6 @@ public Expression makeAggregateCmp(Expression x, AggregateCmpOp op, Expression y // ----------------------------------------------------------------------------------------------------------------- // Pointers - public PointerLiteral makeValue(BigInteger value, PointerType type) { - return new PointerLiteral(type, value); - } - public Expression makeGetElementPointer(Type indexingType, Expression base, List offsets) { Preconditions.checkArgument(base.getType().equals(types.getPointerType()), "Applying offsets to non-pointer expression."); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index 2d40c695dd..9023cb0310 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -63,7 +63,6 @@ public interface ExpressionVisitor { default TRet visitPtrToIntCastExpression(PtrToIntCast expr) { return visitCastExpression(expr); } default TRet visitPtrCmpExpression(PtrCmpExpr expr) { return visitBinaryExpression(expr); } default TRet visitNullLiteral(NullLiteral lit) { return visitLeafExpression(lit); } - default TRet visitPtrLiteral(PointerLiteral lit) {return visitLeafExpression(lit);} // =================================== Generic =================================== default TRet visitITEExpression(ITEExpr expr) { return visitExpression(expr); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerLiteral.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerLiteral.java deleted file mode 100644 index e668e72c0e..0000000000 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerLiteral.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.dat3m.dartagnan.expression.pointer; - -import com.dat3m.dartagnan.expression.ExpressionVisitor; -import com.dat3m.dartagnan.expression.base.LiteralExpressionBase; -import com.dat3m.dartagnan.expression.type.IntegerType; -import com.dat3m.dartagnan.expression.type.PointerType; -import com.google.common.base.Preconditions; - -import java.math.BigInteger; - -public final class PointerLiteral extends LiteralExpressionBase { - - private final BigInteger value; - - public PointerLiteral(PointerType type, BigInteger value) { - super(type); - this.value = value; - } - - public BigInteger getValue() { - return value; - } - - @Override - public T accept(ExpressionVisitor visitor) { - return visitor.visitPtrLiteral(this); - } - - - @Override - public boolean equals(Object o) { - return this == o || o instanceof PointerLiteral val && getType().equals(val.getType()) && value.equals(val.value); - } - - @Override - public String toString() { - return String.format("%s(%s)", getType(), value); - } -} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java index ae56ca6b2b..452bae2f16 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java @@ -390,10 +390,10 @@ public Object visitAsmMetadataEntries(AsmArmParser.AsmMetadataEntriesContext ctx @Override public Object visitValue(AsmArmParser.ValueContext ctx) { - checkState(expectedType instanceof IntegerType || expectedType instanceof PointerType, "Expected type is not an integer type"); + checkState(expectedType instanceof IntegerType, "Expected type is not an integer type"); String valueString = ctx.Numbers().getText(); BigInteger value = new BigInteger(valueString); - return expectedType instanceof IntegerType ? expressions.makeValue(value, (IntegerType) expectedType):expressions.makeValue(value, (PointerType) expectedType); + return expressions.makeValue(value, (IntegerType) expectedType); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java index 372dbe9c16..b69facef3c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java @@ -50,7 +50,7 @@ public class VisitorLlvm extends LLVMIRBaseVisitor { private final Program program; private final TypeFactory types = TypeFactory.getInstance(); private final ExpressionFactory expressions = ExpressionFactory.getInstance(); - private final Type pointerType = types.getPointerType(); + private final PointerType pointerType = types.getPointerType(); private final IntegerType integerType = types.getArchType(); private final Map constantMap = new HashMap<>(); private final Map typeDefinitionMap = new HashMap<>(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java index a9eeb7a068..b8edaddb13 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java @@ -3,6 +3,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.Type; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.program.event.AbstractEvent; import static com.dat3m.dartagnan.program.event.Tag.MEMORY; import static com.dat3m.dartagnan.program.event.Tag.VISIBLE; @@ -19,6 +20,7 @@ public abstract class AbstractMemoryCoreEvent extends AbstractEvent implements M protected Type accessType; public AbstractMemoryCoreEvent(Expression address, Type accessType) { + Preconditions.checkArgument(address.getType() instanceof PointerType); this.address = Preconditions.checkNotNull(address); this.accessType = accessType; addTags(VISIBLE, MEMORY); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index f222dc21d2..93f4912733 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -1654,7 +1654,7 @@ private List inlineMemCpyS(FunctionCall call) { final int destsz = destszValue.getValueAsInt(); // Runtime checks - final Expression nullExpr = expressions.makeZero(types.getArchType()); + final Expression nullExpr = expressions.makeNullLiteral(); final Expression destIsNull = expressions.makeEQ(dest, nullExpr); final Expression srcIsNull = expressions.makeEQ(src, nullExpr); @@ -1669,8 +1669,8 @@ private List inlineMemCpyS(FunctionCall call) { final Expression countGtdestszExpr = expressions.makeGT(castCountExpr, castDestszExpr, false); final Expression invalidCount = expressions.makeOr(countGtMax, countGtdestszExpr); final Expression overlap = expressions.makeAnd( - expressions.makeGT(expressions.makeAdd(src, castCountExpr), dest, false), - expressions.makeGT(expressions.makeAdd(dest, castCountExpr), src, false)); + expressions.makeGT(expressions.makePtrToIntCast(expressions.makePtrAdd(src, castCountExpr)), expressions.makePtrToIntCast(dest), false), + expressions.makeGT(expressions.makePtrToIntCast(expressions.makePtrAdd(dest, castCountExpr)), expressions.makePtrToIntCast(src), false)); final List replacement = new ArrayList<>(); @@ -1707,7 +1707,7 @@ private List inlineMemCpyS(FunctionCall call) { )); for (int i = 0; i < destsz; i++) { final Expression offset = expressions.makeValue(i, types.getArchType()); - final Expression destAddr = expressions.makeAdd(dest, offset); + final Expression destAddr = expressions.makePtrAdd(dest, offset); final Expression zero = expressions.makeZero(types.getArchType()); replacement.add( newStore(destAddr, zero) @@ -1723,8 +1723,8 @@ private List inlineMemCpyS(FunctionCall call) { replacement.add(success); for (int i = 0; i < count; i++) { final Expression offset = expressions.makeValue(i, types.getArchType()); - final Expression srcAddr = expressions.makeAdd(src, offset); - final Expression destAddr = expressions.makeAdd(dest, offset); + final Expression srcAddr = expressions.makePtrAdd(src, offset); + final Expression destAddr = expressions.makePtrAdd(dest, offset); // FIXME: We have no other choice but to load ptr-sized chunks for now final Register reg = caller.getOrNewRegister("__memcpy_" + i, types.getArchType()); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java index 7b91819b7c..fcd8fc4e74 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveDevirtualisation.java @@ -4,11 +4,9 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.integers.IntLiteral; -import com.dat3m.dartagnan.expression.pointer.PointerLiteral; import com.dat3m.dartagnan.expression.processing.ExprTransformer; import com.dat3m.dartagnan.expression.processing.ExpressionInspector; import com.dat3m.dartagnan.expression.type.IntegerType; -import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.IRHelper; @@ -100,12 +98,12 @@ private void findAndTransformAddressTakenFunctions( } } - private boolean assignAddressToFunction(Function func, Map func2AddressMap) { - final PointerType ptrType = TypeFactory.getInstance().getPointerType(); + private boolean assignAddressToFunction(Function func, Map func2AddressMap) { + final IntegerType ptrType = TypeFactory.getInstance().getArchType(); final ExpressionFactory expressions = ExpressionFactory.getInstance(); if (!func2AddressMap.containsKey(func)) { logger.debug("Assigned address \"{}\" to function \"{}\"", nextAvailableFuncAddress, func); - func2AddressMap.put(func, expressions.makeValue(BigInteger.valueOf(nextAvailableFuncAddress), ptrType)); + func2AddressMap.put(func, expressions.makeValue(BigInteger.valueOf(nextAvailableFuncAddress),ptrType)); nextAvailableFuncAddress += 8; return true; } @@ -122,7 +120,7 @@ private void applyTransformerToEvent(Event e, ExpressionVisitor tran } } - private void devirtualise(Function function, Map func2AddressMap) { + private void devirtualise(Function function, Map func2AddressMap) { final ExpressionFactory expressions = ExpressionFactory.getInstance(); int devirtCounter = 0; @@ -150,9 +148,9 @@ private void devirtualise(Function function, Map func2 final Expression funcPtr = call.getCallTarget(); // Construct call table for (Function possibleTarget : possibleTargets) { - final PointerLiteral targetAddress = func2AddressMap.get(possibleTarget); + final IntLiteral targetAddress = func2AddressMap.get(possibleTarget); final Label caseLabel = EventFactory.newLabel(String.format("__Ldevirt_%s#%s", targetAddress.getValue(), devirtCounter)); - final CondJump caseJump = EventFactory.newJump(expressions.makeEQ(funcPtr, targetAddress), caseLabel); + final CondJump caseJump = EventFactory.newJump(expressions.makeEQ(expressions.makePtrToIntCast(funcPtr), targetAddress), caseLabel); caseLabels.add(caseLabel); caseJumps.add(caseJump); } @@ -181,7 +179,7 @@ private boolean needsDevirtualization(CallEvent call) { return !call.isDirectCall(); } - private List getPossibleTargets(CallEvent call, Map func2AddressMap) { + private List getPossibleTargets(CallEvent call, Map func2AddressMap) { Preconditions.checkArgument(needsDevirtualization(call)); return func2AddressMap.keySet().stream() .filter(f -> f.getFunctionType() == call.getCallType()) @@ -213,7 +211,7 @@ public Expression visitFunction(Function function) { private static class FunctionToAddressTransformer extends ExprTransformer { - private final Map func2AddressMap = new HashMap<>(); + private final Map func2AddressMap = new HashMap<>(); @Override public Expression visitFunction(Function function) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java index a6585245b2..253db9bcd6 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java @@ -187,10 +187,6 @@ private void resolveDynamicThreadJoin(Program program, List threadDa final Expression tidExpr_ = join.getTid(); final Expression tidExpr = tidExpr_.getType() instanceof PointerType ? expressions.makePtrToIntCast(tidExpr_) : tidExpr_; - - - - final Register joinRegister = join.getResultRegister(); final IntegerType statusType = (IntegerType) ((AggregateType)joinRegister.getType()).getFields().get(0).type(); final Type retValType = ((AggregateType)joinRegister.getType()).getFields().get(1).type(); @@ -342,11 +338,18 @@ private void resolvePthreadSelf(Program program) { } if (call.getCalledFunction().getIntrinsicInfo() == Intrinsics.Info.P_THREAD_SELF) { final Register resultRegister = getResultRegister(call); - assert resultRegister.getType() instanceof IntegerType; + Type regType = resultRegister.getType(); + assert regType instanceof PointerType || regType instanceof IntegerType; assert call.getArguments().isEmpty(); - final Expression tidExpr = new TIdExpr((IntegerType) resultRegister.getType(), call.getThread()); - final Local tidAssignment = newLocal(resultRegister, tidExpr); - IRHelper.replaceWithMetadata(call, tidAssignment); + if (regType instanceof PointerType) { + final Expression tidExpr = new TIdExpr(archType, call.getThread()); + final Local tidAssignment = newLocal(resultRegister, expressions.makeIntToPtrCast(tidExpr)); + IRHelper.replaceWithMetadata(call, tidAssignment); + }else { + final Expression tidExpr = new TIdExpr((IntegerType) regType, call.getThread()); + final Local tidAssignment = newLocal(resultRegister, tidExpr); + IRHelper.replaceWithMetadata(call, tidAssignment); + } } } } @@ -660,5 +663,4 @@ public T accept(ExpressionVisitor visitor) { } } - -} +} \ No newline at end of file From ba0401423734308ad145cf597c0163fca0e40a27 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 20 Oct 2025 11:50:52 +0200 Subject: [PATCH 10/33] fixed litmus c --- .../expression/ExpressionFactory.java | 48 ++++++++++++++----- .../visitors/VisitorLitmusAArch64.java | 3 ++ .../program/visitors/VisitorLitmusC.java | 4 +- .../parsers/program/visitors/VisitorLlvm.java | 8 ++-- .../event/core/AbstractMemoryCoreEvent.java | 1 + .../dartagnan/program/processing/Tearing.java | 30 ++++++++---- 6 files changed, 66 insertions(+), 28 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index 6a1e85d8eb..3257f59bed 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -106,31 +106,35 @@ public IntLiteral makeValue(BigInteger value, IntegerType type) { } public Expression makeLT(Expression leftOperand, Expression rightOperand, boolean signed) { - if (leftOperand.getType() instanceof PointerType){ - return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.LT : IntCmpOp.ULT, makePtrToIntCast(rightOperand)); - } return makeIntCmp(leftOperand, signed ? IntCmpOp.LT : IntCmpOp.ULT, rightOperand); } public Expression makeGT(Expression leftOperand, Expression rightOperand, boolean signed) { - if (leftOperand.getType() instanceof PointerType){ - return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.GT : IntCmpOp.UGT, makePtrToIntCast(rightOperand)); - } return makeIntCmp(leftOperand, signed ? IntCmpOp.GT : IntCmpOp.UGT, rightOperand); } public Expression makeLTE(Expression leftOperand, Expression rightOperand, boolean signed) { - if (leftOperand.getType() instanceof PointerType){ - return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.LTE : IntCmpOp.ULTE, makePtrToIntCast(rightOperand)); - } return makeIntCmp(leftOperand, signed ? IntCmpOp.LTE : IntCmpOp.ULTE, rightOperand); } public Expression makeGTE(Expression leftOperand, Expression rightOperand, boolean signed) { - if (leftOperand.getType() instanceof PointerType){ - return makeIntCmp(makePtrToIntCast(leftOperand), signed ? IntCmpOp.GTE : IntCmpOp.UGTE, makePtrToIntCast(rightOperand)); - } return makeIntCmp(leftOperand, signed ? IntCmpOp.GTE : IntCmpOp.UGTE, rightOperand); + + } + public Expression makeLTCompatibility(Expression leftOperand, Expression rightOperand, boolean signed) { + return makeIntCmpCompatibility(leftOperand, signed ? IntCmpOp.LT : IntCmpOp.ULT, rightOperand); + } + + public Expression makeGTCompatibility(Expression leftOperand, Expression rightOperand, boolean signed) { + return makeIntCmpCompatibility(leftOperand, signed ? IntCmpOp.GT : IntCmpOp.UGT, rightOperand); + } + + public Expression makeLTECompatibility(Expression leftOperand, Expression rightOperand, boolean signed) { + return makeIntCmpCompatibility(leftOperand, signed ? IntCmpOp.LTE : IntCmpOp.ULTE, rightOperand); + } + + public Expression makeGTECompatibility(Expression leftOperand, Expression rightOperand, boolean signed) { + return makeIntCmpCompatibility(leftOperand, signed ? IntCmpOp.GTE : IntCmpOp.UGTE, rightOperand); } public Expression makeNeg(Expression operand) { @@ -201,10 +205,30 @@ public Expression makeIntCmp(Expression leftOperand, IntCmpOp operator, Expressi return new IntCmpExpr(types.getBooleanType(), leftOperand, operator, rightOperand); } + public Expression makeIntCmpCompatibility(Expression leftOperand, IntCmpOp operator, Expression rightOperand) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntCmpCompatibility(makePtrToIntCast(leftOperand), operator, rightOperand); + } + if (rightOperand.getType() instanceof PointerType){ + return makeIntCmpCompatibility(leftOperand, operator, makePtrToIntCast(rightOperand)); + } + return new IntCmpExpr(types.getBooleanType(), leftOperand, operator, rightOperand); + } + public Expression makeIntBinary(Expression leftOperand, IntBinaryOp operator, Expression rightOperand) { return new IntBinaryExpr(leftOperand, operator, rightOperand); } + public Expression makeIntBinaryCompatibility(Expression leftOperand, IntBinaryOp operator, Expression rightOperand) { + if (leftOperand.getType() instanceof PointerType){ + return makeIntBinaryCompatibility(makePtrToIntCast(leftOperand), operator, rightOperand); + } + if (rightOperand.getType() instanceof PointerType){ + return makeIntBinaryCompatibility(leftOperand, operator, makePtrToIntCast(rightOperand)); + } + return new IntBinaryExpr(leftOperand, operator, rightOperand); + } + public Expression makeIntegerCast(Expression operand, IntegerType targetType, boolean signed) { final Type sourceType = operand.getType(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java index 9e6174780b..ce658a9865 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java @@ -6,6 +6,7 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.integers.IntLiteral; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.parsers.LitmusAArch64BaseVisitor; import com.dat3m.dartagnan.parsers.LitmusAArch64Parser.*; @@ -36,6 +37,7 @@ private record CmpInstruction(Expression left, Expression right) {} private final IntegerType i16 = types.getIntegerType(16); private final IntegerType i32 = types.getIntegerType(32); private final IntegerType i64 = types.getIntegerType(64); + private final PointerType pointerType = types.getPointerType(); private final ExpressionFactory expressions = programBuilder.getExpressionFactory(); private int mainThread; private int threadCount = 0; @@ -428,6 +430,7 @@ private IntLiteral parseValue(ConstantContext ctx, IntegerType type) { return expressions.parseValue(ctx.getText(), type); } + private Register shrinkRegister(Register other, Register32Context ctx, boolean halfWordSize, boolean byteSize) { checkArgument(other.getType().equals(i64), "Non-64-bit %s", other); checkArgument(!byteSize | !halfWordSize, "Inconclusive access size"); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index be67a46f6c..821b58549e 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -467,7 +467,7 @@ public Expression visitReOpArith(LitmusCParser.ReOpArithContext ctx){ Register register = getReturnRegister(false); Expression v1 = (Expression)ctx.re(0).accept(this); Expression v2 = (Expression)ctx.re(1).accept(this); - Expression result = expressions.makeIntBinary(v1, ctx.opArith().op, v2); + Expression result = expressions.makeIntBinaryCompatibility(v1, ctx.opArith().op, v2); return assignToReturnRegister(register, result); } @@ -691,7 +691,7 @@ public Expression visitVarName(LitmusCParser.VarNameContext ctx){ private Expression getAddress(LitmusCParser.ReContext ctx){ Expression address = (Expression)ctx.accept(this); if(address.getType() instanceof IntegerType){ - return address; //todo: remove + return expressions.makeIntToPtrCast(address); } if(address.getType() instanceof PointerType){ return address; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java index b69facef3c..7fc778c6a9 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java @@ -587,10 +587,10 @@ public Expression visitICmpInst(ICmpInstContext ctx) { final Expression compared = switch (operator) { case "eq" -> expressions.makeEQ(left, right); case "ne" -> expressions.makeNEQ(left, right); - case "slt", "ult" -> expressions.makeLT(left, right, operator.startsWith("s")); - case "sle", "ule" -> expressions.makeLTE(left, right, operator.startsWith("s")); - case "sgt", "ugt" -> expressions.makeGT(left, right, operator.startsWith("s")); - case "sge", "uge" -> expressions.makeGTE(left, right, operator.startsWith("s")); + case "slt", "ult" -> expressions.makeLTCompatibility(left, right, operator.startsWith("s")); + case "sle", "ule" -> expressions.makeLTECompatibility(left, right, operator.startsWith("s")); + case "sgt", "ugt" -> expressions.makeGTCompatibility(left, right, operator.startsWith("s")); + case "sge", "uge" -> expressions.makeGTECompatibility(left, right, operator.startsWith("s")); default -> throw new ParsingException(String.format("Unknown predicate in %s.", ctx.getText())); }; // LLVM does not support a distinct boolean type. diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java index b8edaddb13..bd31609488 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java @@ -4,6 +4,7 @@ import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.type.PointerType; +import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.event.AbstractEvent; import static com.dat3m.dartagnan.program.event.Tag.MEMORY; import static com.dat3m.dartagnan.program.event.Tag.VISIBLE; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java index 31d190f021..ca4491fcc7 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java @@ -6,6 +6,7 @@ import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.processing.ExprTransformer; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.IRHelper; @@ -173,9 +174,9 @@ private void tearExpressions(Program program) { private List createTransaction(Load load, List offsets) { final int bytes = checkBytes(load, offsets); final List replacement = new ArrayList<>(); - final IntegerType addressType = checkIntegerType(load.getAddress().getType(), - "Non-integer address in '%s'", load); - checkIntegerType(load.getAccessType(), "Non-integer mixed-size access in '%s'", load); + final PointerType addressType = checkPointerType(load.getAddress().getType(), + "Non-Pointer address in '%s'", load); + checkType(load.getAccessType(), "Non-integer or Pointer mixed-size access in '%s'", load); final Function function = load.getFunction(); final Register addressRegister = toRegister(load.getAddress(), function, replacement); final List smallerRegisters = new ArrayList<>(); @@ -192,8 +193,8 @@ private List createTransaction(Load load, List offsets) { } for (int i = -1; i < offsets.size(); i++) { final int start = i < 0 ? 0 : offsets.get(i); - final Expression offset = expressions.makeValue(start, addressType); - final Expression address = expressions.makeAdd(addressRegister, offset); + final Expression offset = expressions.makeValue(start, types.getArchType()); + final Expression address = expressions.makePtrAdd(addressRegister, offset); final Load byteLoad = load.getCopy(); final Register result = smallerRegisters.get(i + 1); byteLoad.setResultRegister(result); @@ -214,9 +215,9 @@ private List createTransaction(Load load, List offsets) { private List createTransaction(Store store, List offsets, Map> map, boolean bigEndian) { final int bytes = checkBytes(store, offsets); final List replacement = new ArrayList<>(); - final IntegerType addressType = checkIntegerType(store.getAddress().getType(), - "Non-integer address in '%s'", store); - checkIntegerType(store.getAccessType(), "Non-integer mixed-size access in '%s'", store); + final PointerType addressType = checkPointerType(store.getAddress().getType(), + "Non-Pointer address in '%s'", store); + checkType(store.getAccessType(), "Non-integer or Pointer mixed-size access in '%s'", store); final Function function = store.getFunction(); final Register addressRegister = toRegister(store.getAddress(), function, replacement); final Register valueRegister = toRegister(store.getMemValue(), function, replacement); @@ -231,7 +232,7 @@ private List createTransaction(Store store, List offsets, Map createTransaction(Store store, List offsets, Map Date: Mon, 20 Oct 2025 17:54:12 +0200 Subject: [PATCH 11/33] more fixes --- .../expression/ExpressionFactory.java | 28 +++++++++---------- .../program/visitors/VisitorLitmusC.java | 4 +-- .../parsers/program/visitors/VisitorLlvm.java | 8 +++--- .../event/core/AbstractMemoryCoreEvent.java | 2 +- .../others/miscellaneous/AnalysisTest.java | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index 3257f59bed..01c12bf850 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -121,20 +121,20 @@ public Expression makeGTE(Expression leftOperand, Expression rightOperand, boole return makeIntCmp(leftOperand, signed ? IntCmpOp.GTE : IntCmpOp.UGTE, rightOperand); } - public Expression makeLTCompatibility(Expression leftOperand, Expression rightOperand, boolean signed) { - return makeIntCmpCompatibility(leftOperand, signed ? IntCmpOp.LT : IntCmpOp.ULT, rightOperand); + public Expression makeLTforced(Expression leftOperand, Expression rightOperand, boolean signed) { + return makeIntCmpForced(leftOperand, signed ? IntCmpOp.LT : IntCmpOp.ULT, rightOperand); } - public Expression makeGTCompatibility(Expression leftOperand, Expression rightOperand, boolean signed) { - return makeIntCmpCompatibility(leftOperand, signed ? IntCmpOp.GT : IntCmpOp.UGT, rightOperand); + public Expression makeGTforced(Expression leftOperand, Expression rightOperand, boolean signed) { + return makeIntCmpForced(leftOperand, signed ? IntCmpOp.GT : IntCmpOp.UGT, rightOperand); } - public Expression makeLTECompatibility(Expression leftOperand, Expression rightOperand, boolean signed) { - return makeIntCmpCompatibility(leftOperand, signed ? IntCmpOp.LTE : IntCmpOp.ULTE, rightOperand); + public Expression makeLTEforced(Expression leftOperand, Expression rightOperand, boolean signed) { + return makeIntCmpForced(leftOperand, signed ? IntCmpOp.LTE : IntCmpOp.ULTE, rightOperand); } - public Expression makeGTECompatibility(Expression leftOperand, Expression rightOperand, boolean signed) { - return makeIntCmpCompatibility(leftOperand, signed ? IntCmpOp.GTE : IntCmpOp.UGTE, rightOperand); + public Expression makeGTEforced(Expression leftOperand, Expression rightOperand, boolean signed) { + return makeIntCmpForced(leftOperand, signed ? IntCmpOp.GTE : IntCmpOp.UGTE, rightOperand); } public Expression makeNeg(Expression operand) { @@ -205,12 +205,12 @@ public Expression makeIntCmp(Expression leftOperand, IntCmpOp operator, Expressi return new IntCmpExpr(types.getBooleanType(), leftOperand, operator, rightOperand); } - public Expression makeIntCmpCompatibility(Expression leftOperand, IntCmpOp operator, Expression rightOperand) { + public Expression makeIntCmpForced(Expression leftOperand, IntCmpOp operator, Expression rightOperand) { if (leftOperand.getType() instanceof PointerType){ - return makeIntCmpCompatibility(makePtrToIntCast(leftOperand), operator, rightOperand); + return makeIntCmpForced(makePtrToIntCast(leftOperand), operator, rightOperand); } if (rightOperand.getType() instanceof PointerType){ - return makeIntCmpCompatibility(leftOperand, operator, makePtrToIntCast(rightOperand)); + return makeIntCmpForced(leftOperand, operator, makePtrToIntCast(rightOperand)); } return new IntCmpExpr(types.getBooleanType(), leftOperand, operator, rightOperand); } @@ -219,12 +219,12 @@ public Expression makeIntBinary(Expression leftOperand, IntBinaryOp operator, Ex return new IntBinaryExpr(leftOperand, operator, rightOperand); } - public Expression makeIntBinaryCompatibility(Expression leftOperand, IntBinaryOp operator, Expression rightOperand) { + public Expression makeIntBinaryForced(Expression leftOperand, IntBinaryOp operator, Expression rightOperand) { if (leftOperand.getType() instanceof PointerType){ - return makeIntBinaryCompatibility(makePtrToIntCast(leftOperand), operator, rightOperand); + return makeIntBinaryForced(makePtrToIntCast(leftOperand), operator, rightOperand); } if (rightOperand.getType() instanceof PointerType){ - return makeIntBinaryCompatibility(leftOperand, operator, makePtrToIntCast(rightOperand)); + return makeIntBinaryForced(leftOperand, operator, makePtrToIntCast(rightOperand)); } return new IntBinaryExpr(leftOperand, operator, rightOperand); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index 821b58549e..810c8928a4 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -458,7 +458,7 @@ public Expression visitReOpCompare(LitmusCParser.ReOpCompareContext ctx){ Register register = getReturnRegister(false); Expression v1 = (Expression)ctx.re(0).accept(this); Expression v2 = (Expression)ctx.re(1).accept(this); - Expression result = expressions.makeIntCmp(v1, ctx.opCompare().op, v2); + Expression result = expressions.makeIntCmpForced(v1, ctx.opCompare().op, v2); return assignToReturnRegister(register, result); } @@ -467,7 +467,7 @@ public Expression visitReOpArith(LitmusCParser.ReOpArithContext ctx){ Register register = getReturnRegister(false); Expression v1 = (Expression)ctx.re(0).accept(this); Expression v2 = (Expression)ctx.re(1).accept(this); - Expression result = expressions.makeIntBinaryCompatibility(v1, ctx.opArith().op, v2); + Expression result = expressions.makeIntBinaryForced(v1, ctx.opArith().op, v2); return assignToReturnRegister(register, result); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java index 7fc778c6a9..cc5805d572 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java @@ -587,10 +587,10 @@ public Expression visitICmpInst(ICmpInstContext ctx) { final Expression compared = switch (operator) { case "eq" -> expressions.makeEQ(left, right); case "ne" -> expressions.makeNEQ(left, right); - case "slt", "ult" -> expressions.makeLTCompatibility(left, right, operator.startsWith("s")); - case "sle", "ule" -> expressions.makeLTECompatibility(left, right, operator.startsWith("s")); - case "sgt", "ugt" -> expressions.makeGTCompatibility(left, right, operator.startsWith("s")); - case "sge", "uge" -> expressions.makeGTECompatibility(left, right, operator.startsWith("s")); + case "slt", "ult" -> expressions.makeLTforced(left, right, operator.startsWith("s")); + case "sle", "ule" -> expressions.makeLTEforced(left, right, operator.startsWith("s")); + case "sgt", "ugt" -> expressions.makeGTforced(left, right, operator.startsWith("s")); + case "sge", "uge" -> expressions.makeGTEforced(left, right, operator.startsWith("s")); default -> throw new ParsingException(String.format("Unknown predicate in %s.", ctx.getText())); }; // LLVM does not support a distinct boolean type. diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java index bd31609488..3cab8ec083 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java @@ -21,7 +21,7 @@ public abstract class AbstractMemoryCoreEvent extends AbstractEvent implements M protected Type accessType; public AbstractMemoryCoreEvent(Expression address, Type accessType) { - Preconditions.checkArgument(address.getType() instanceof PointerType); + // Preconditions.checkArgument(address.getType() instanceof PointerType); // address can be a register this.address = Preconditions.checkNotNull(address); this.accessType = accessType; addTags(VISIBLE, MEMORY); diff --git a/dartagnan/src/test/java/com/dat3m/dartagnan/others/miscellaneous/AnalysisTest.java b/dartagnan/src/test/java/com/dat3m/dartagnan/others/miscellaneous/AnalysisTest.java index 28c654bd66..f441869e59 100644 --- a/dartagnan/src/test/java/com/dat3m/dartagnan/others/miscellaneous/AnalysisTest.java +++ b/dartagnan/src/test/java/com/dat3m/dartagnan/others/miscellaneous/AnalysisTest.java @@ -593,7 +593,7 @@ public void fullPropagation1() throws InvalidConfigurationException { Load e0 = newLoad(r0, y); // reads x b.addChild(0, e0); Label l0 = newLabel("l0"); - b.addChild(0, newJump(expressions.makeEQ(r0, x), l0)); + b.addChild(0, newJump(expressions.makeEQ(r0, expressions.makePtrToIntCast(x)), l0)); Load e1 = newLoad(r0, x); // reads y b.addChild(0, e1); b.addChild(0, l0); From b89319605c081235a145b63836ee8793ff8edb90 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 27 Oct 2025 09:02:28 +0100 Subject: [PATCH 12/33] Pointer size and visitor fixes --- .../expression/ExpressionFactory.java | 11 +++++++++++ .../dartagnan/expression/type/PointerType.java | 9 ++++++++- .../dartagnan/expression/type/TypeFactory.java | 8 +++++++- .../program/visitors/VisitorLitmusAArch64.java | 4 ++-- .../program/visitors/VisitorLitmusC.java | 2 +- .../program/visitors/VisitorLitmusPPC.java | 4 ++-- .../event/common/SingleAccessMemoryEvent.java | 2 ++ .../event/core/AbstractMemoryCoreEvent.java | 2 +- .../dartagnan/program/processing/Inlining.java | 18 +++++++++++++++++- 9 files changed, 51 insertions(+), 9 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index 01c12bf850..47b6134414 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -383,6 +383,16 @@ public Expression makePtrAdd(Expression base, Expression offset) { return new PointerAddExpr(base, offset); } + public Expression makePtrCast(Expression base,PointerType type){ + if (base.getType() instanceof PointerType ) { + return base; + } + if (base.getType() instanceof IntegerType) { + return makeIntToPtrCast(makeCast(base, types.getArchType())); + } + throw new UnsupportedOperationException(String.format("Cast %s into pointer unsupported.",base)); + } + public Expression makePtrToIntCast(Expression pointer) { return new PtrToIntCast(types.getArchType(), pointer); @@ -397,6 +407,7 @@ public Expression makeIntToPtrCast(Expression operand) { } + public Expression makeNullLiteral(PointerType pointerType) { return new NullLiteral(pointerType); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java index 67513c88b0..6c62ac306b 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -4,11 +4,18 @@ public class PointerType implements Type { + public int size = 64; + PointerType() {} + PointerType(int size) { + this.size = size; + } + + @Override public String toString() { - return "ptr"; + return "ptr_"+ size; } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java index 905ddb2168..b12ad68591 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java @@ -21,11 +21,12 @@ public final class TypeFactory { private final VoidType voidType = new VoidType(); private final BooleanType booleanType = new BooleanType(); private final IntegerType archType; - private final PointerType pointerType = new PointerType(); + private final PointerType pointerType; private final Normalizer typeNormalizer = new Normalizer(); private TypeFactory() { archType = getIntegerType(64); + pointerType = getPointerType(64); } @@ -43,6 +44,11 @@ public BooleanType getBooleanType() { public PointerType getPointerType() { return pointerType; } + public PointerType getPointerType(int bitWidth) { + // to be used in tearing + checkArgument(bitWidth > 0, "Non-positive bit width %s.", bitWidth); + return typeNormalizer.normalize(new PointerType(bitWidth)); + } public IntegerType getIntegerType(int bitWidth) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java index ce658a9865..19ef9b902a 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java @@ -182,7 +182,7 @@ public Object visitLoad(LoadContext ctx) { final Register register = shrinkRegister(r64, ctx.rD32, inst.halfWordSize, inst.byteSize); final Expression address = parseAddress(ctx.address()); final String mo = inst.acquire ? MO_ACQ : ""; - add(EventFactory.newLoadWithMo(register, address, mo)); + add(EventFactory.newLoadWithMo(register, expressions.makePtrCast(address,pointerType), mo)); addRegister64Update(r64, register); return null; } @@ -389,7 +389,7 @@ private Expression parseExpression(Expr32Context x32, Expr64Context x64) { private Expression parseAddress(AddressContext ctx) { final Register base = programBuilder.getOrErrorRegister(mainThread, ctx.register64().id); if (ctx.offset() == null) { - return base; + return expressions.makePtrCast(base,pointerType); } final ExpressionConversionContext conversion = ctx.offset().expressionConversion(); final Register32Context register32 = conversion == null ? null : conversion.register32(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index 810c8928a4..83a27d6597 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -587,7 +587,7 @@ public Object visitNreAssignment(LitmusCParser.NreAssignmentContext ctx){ Expression value = (Expression)ctx.re().accept(this); if(variable instanceof MemoryObject || variable instanceof Register){ - Event event = EventFactory.newStoreWithMo(variable, value, C11.NONATOMIC); + Event event = EventFactory.newStoreWithMo(variable, expressions.makePtrCast(value,pointerType), C11.NONATOMIC); if (isOpenCL) { event.addTags(Tag.OpenCL.DEFAULT_WEAK_SCOPE); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPPC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPPC.java index 6dc700bb5e..e73114b282 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPPC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusPPC.java @@ -131,7 +131,7 @@ public Object visitLwarx(LitmusPPCParser.LwarxContext ctx) { Register r1 = (Register) ctx.register(0).accept(this); Register ra = (Register) ctx.register(1).accept(this); Register rb = (Register) ctx.register(2).accept(this); - return programBuilder.addChild(mainThread, EventFactory.newRMWLoadExclusive(r1, expressions.makeAdd(ra, rb))); + return programBuilder.addChild(mainThread, EventFactory.newRMWLoadExclusive(r1, expressions.makePtrAdd(rb, ra))); } @Override @@ -156,7 +156,7 @@ public Object visitStwcx(LitmusPPCParser.StwcxContext ctx) { Register r1 = (Register) ctx.register(0).accept(this); Register ra = (Register) ctx.register(1).accept(this); Register rb = (Register) ctx.register(2).accept(this); - return programBuilder.addChild(mainThread, EventFactory.Common.newExclusiveStore(rs, expressions.makeAdd(ra, rb), r1, "")); + return programBuilder.addChild(mainThread, EventFactory.Common.newExclusiveStore(rs, expressions.makePtrAdd(rb, ra), r1, "")); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/common/SingleAccessMemoryEvent.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/common/SingleAccessMemoryEvent.java index 5ebb7129f5..f9d07e5e0c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/common/SingleAccessMemoryEvent.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/common/SingleAccessMemoryEvent.java @@ -3,6 +3,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.Type; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.program.event.AbstractEvent; import com.dat3m.dartagnan.program.event.MemoryAccess; import com.dat3m.dartagnan.program.event.MemoryEvent; @@ -32,6 +33,7 @@ public abstract class SingleAccessMemoryEvent extends AbstractEvent implements M // The empty string means no memory order public SingleAccessMemoryEvent(Expression address, Type accessType, String mo) { Preconditions.checkNotNull(mo, "The memory ordering cannot be null"); + Preconditions.checkArgument(address.getType() instanceof PointerType); this.address = address; this.mo = mo; this.accessType = accessType; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java index 3cab8ec083..bd31609488 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/core/AbstractMemoryCoreEvent.java @@ -21,7 +21,7 @@ public abstract class AbstractMemoryCoreEvent extends AbstractEvent implements M protected Type accessType; public AbstractMemoryCoreEvent(Expression address, Type accessType) { - // Preconditions.checkArgument(address.getType() instanceof PointerType); // address can be a register + Preconditions.checkArgument(address.getType() instanceof PointerType); this.address = Preconditions.checkNotNull(address); this.accessType = accessType; addTags(VISIBLE, MEMORY); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java index 33d4717860..7f89b95a3b 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java @@ -2,10 +2,14 @@ import com.dat3m.dartagnan.exception.MalformedProgramException; import com.dat3m.dartagnan.expression.Expression; +import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.program.Thread; import com.dat3m.dartagnan.program.*; import com.dat3m.dartagnan.program.event.Event; import com.dat3m.dartagnan.program.event.EventFactory; +import com.dat3m.dartagnan.expression.ExpressionFactory; + import com.dat3m.dartagnan.program.event.Tag; import com.dat3m.dartagnan.program.event.core.ControlBarrier; import com.dat3m.dartagnan.program.event.core.Label; @@ -29,6 +33,8 @@ import static com.dat3m.dartagnan.program.event.EventFactory.*; import static com.google.common.base.Verify.verify; + + @Options public class Inlining implements ProgramProcessor { @@ -37,6 +43,9 @@ public class Inlining implements ProgramProcessor { secure = true) private int bound = 1; + private static final ExpressionFactory expressions = ExpressionFactory.getInstance(); + + private Inlining() {} public static Inlining fromConfig(Configuration config) throws InvalidConfigurationException { @@ -129,7 +138,14 @@ private static Event inlineBody(FunctionCall call, Snapshot callTarget, int scop final ArrayList parameterAssignments = new ArrayList<>(); for (int j = 0; j < callTarget.parameters.size(); j++) { final Register register = registerMap.get(callTarget.parameters.get(j)); - parameterAssignments.add(newLocal(register, arguments.get(j))); + if (register.getType() instanceof PointerType) { + Expression v = expressions.makePtrCast(arguments.get(j),(PointerType) register.getType()); + parameterAssignments.add(newLocal(register, v)); + }else{ + Expression v = arguments.get(j); + parameterAssignments.add(newLocal(register, v)); + } + // todo check } // --------- Inline call --------- From 9d48cb71dc6c3897ceb5d6e42a2ce8f96647d73d Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 27 Oct 2025 09:58:08 +0100 Subject: [PATCH 13/33] Vulkan modification, tests pass --- .../dartagnan/expression/ExpressionFactory.java | 4 ++-- .../dartagnan/expression/type/PointerType.java | 13 +++++++------ .../expression/type/ScopedPointerType.java | 2 +- .../program/visitors/spirv/VisitorOpsAtomic.java | 5 +++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index 47b6134414..b82e46339a 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -354,7 +354,7 @@ public Expression makeAggregateCmp(Expression x, AggregateCmpOp op, Expression y // Pointers public Expression makeGetElementPointer(Type indexingType, Expression base, List offsets) { - Preconditions.checkArgument(base.getType().equals(types.getPointerType()), + Preconditions.checkArgument(base.getType() instanceof PointerType, "Applying offsets to non-pointer expression."); return new GEPExpr(indexingType, base, offsets,null); } @@ -363,7 +363,7 @@ public Expression makeGetElementPointer(Type indexingType, Expression base, List // TODO: Stride should be a property of the pointer, not of a GEPExpr. // Refactor GEPExpr to only accept a (new) PointerType and a list of offsets. // A PointerType should have the referred type and the stride in its attributes. - Preconditions.checkArgument(base.getType().equals(types.getPointerType()), + Preconditions.checkArgument(base.getType() instanceof PointerType, "Applying offsets to non-pointer expression."); Preconditions.checkArgument(stride == null || stride >= types.getMemorySizeInBytes(indexingType), "Stride cannot be smaller than indexing type"); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java index 6c62ac306b..f1f41daf1a 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -4,23 +4,24 @@ public class PointerType implements Type { - public int size = 64; + public int bitWidth = 64; - PointerType() {} + // todo change the encoding to recognise bitwidth - PointerType(int size) { - this.size = size; + PointerType(int bitwidth) { + this.bitWidth = bitwidth; } @Override public String toString() { - return "ptr_"+ size; + return "ptr_"+ bitWidth; } @Override public boolean equals(Object obj) { - return obj != null && obj.getClass() == this.getClass(); + return obj instanceof PointerType other + && other.bitWidth == this.bitWidth; } } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/ScopedPointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/ScopedPointerType.java index 87125ff27a..eac26595d5 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/ScopedPointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/ScopedPointerType.java @@ -4,7 +4,7 @@ import java.util.Objects; -public class ScopedPointerType extends IntegerType { +public class ScopedPointerType extends PointerType { private static final int ARCH_SIZE = TypeFactory.getInstance().getArchType().getBitWidth(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsAtomic.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsAtomic.java index 77aaebc188..0000843193 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsAtomic.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsAtomic.java @@ -7,6 +7,7 @@ import com.dat3m.dartagnan.expression.integers.IntBinaryOp; import com.dat3m.dartagnan.expression.integers.IntCmpOp; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.parsers.SpirvBaseVisitor; import com.dat3m.dartagnan.parsers.SpirvParser; import com.dat3m.dartagnan.parsers.program.visitors.spirv.helpers.HelperTags; @@ -143,8 +144,8 @@ private Event visitOpAtomicExtremum( String scope = getScopeTag(scopeCtx.getText()); Set tags = getMemorySemanticsTags(tagsCtx.getText()); tags.add(builder.getPointerStorageClass(ptrCtx.getText())); - if (!(ptr.getType() instanceof IntegerType) || !(value.getType() instanceof IntegerType)) { - throw new ParsingException("Unexpected type at '%s' or '%s', expected integer but received '%s' and '%s'", + if (!(ptr.getType() instanceof PointerType) || !(value.getType() instanceof IntegerType)) { + throw new ParsingException("Unexpected type at '%s' or '%s', expected pointer and integer but received '%s' and '%s'", ptrCtx.getText(), valCtx.getText(), ptr.getType(), value.getType()); } SpirvRmwExtremum event = newSpirvRmwExtremum(register, ptr, kind, value, scope, tags); From 24ac314209559a5116f7e8699ddbc33d19506810 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 27 Oct 2025 10:00:17 +0100 Subject: [PATCH 14/33] Vulkan modification, all Vulkan tests pass --- .../com/dat3m/dartagnan/expression/type/PointerType.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java index f1f41daf1a..667b1bb3fb 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -4,12 +4,12 @@ public class PointerType implements Type { - public int bitWidth = 64; + public int bitWidth; - // todo change the encoding to recognise bitwidth + // todo change the encoding to recognise bit width - PointerType(int bitwidth) { - this.bitWidth = bitwidth; + PointerType(int bitWidth) { + this.bitWidth = bitWidth; } From da9ee17fc040c7d245fd129530c474bb5f7d86cd Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 27 Oct 2025 10:15:27 +0100 Subject: [PATCH 15/33] opencl modification --- .../com/dat3m/dartagnan/expression/type/TypeFactory.java | 2 +- .../program/processing/transformers/MemoryTransformer.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java index b12ad68591..2a0ec6cfc8 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java @@ -275,7 +275,7 @@ public Map decomposeIntoPrimitives(Type type) { } public static boolean isStaticType(Type type) { - if (type instanceof BooleanType || type instanceof IntegerType || type instanceof FloatType) { + if (type instanceof BooleanType || type instanceof IntegerType || type instanceof PointerType || type instanceof FloatType) { return true; } if (type instanceof ArrayType aType) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/transformers/MemoryTransformer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/transformers/MemoryTransformer.java index ce5b116e13..d3580517b0 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/transformers/MemoryTransformer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/transformers/MemoryTransformer.java @@ -3,6 +3,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.processing.ExprTransformer; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.ScopedPointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.parsers.program.visitors.spirv.decorations.BuiltIn; @@ -26,6 +27,7 @@ public class MemoryTransformer extends ExprTransformer { // Thread / Subgroup / Workgroup / QueueFamily / Device private static final List namePrefixes = List.of("T", "S", "W", "Q", "D"); private static final Type archType = TypeFactory.getInstance().getArchType(); + private static final Type pointerType = TypeFactory.getInstance().getPointerType(); private final Program program; private final Function function; @@ -67,8 +69,7 @@ public void setThread(Thread thread) { builtIn.setThreadId(tid); registerMapping = function.getRegisters().stream().collect( toMap(r -> r, r -> { - Type type = r.getType() instanceof ScopedPointerType ? archType : r.getType(); - return thread.getOrNewRegister(r.getName(), type); + return thread.getOrNewRegister(r.getName(),r.getType()); })); nonDetMapping = new HashMap<>(); } From 9515dad387c0bf47065ee3b2182f5eae9a09abcb Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Wed, 5 Nov 2025 09:10:16 +0100 Subject: [PATCH 16/33] dev merge --- .../dartagnan/expression/ExpressionFactory.java | 6 ++++++ .../expression/pointer/PointerAddExpr.java | 3 ++- .../dat3m/dartagnan/parsers/cat/VisitorCat.java | 5 +++++ .../parsers/program/visitors/VisitorAsmArm.java | 17 ++++++++++++----- .../program/visitors/VisitorAsmRISCV.java | 13 ++++++++++--- .../program/visitors/VisitorLitmusC.java | 13 +++++++------ 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index b82e46339a..e4292d1cdc 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -78,6 +78,8 @@ public Expression makeBooleanCast(Expression operand) { return operand; } else if (sourceType instanceof IntegerType intType) { return makeNEQ(operand, makeZero(intType)); + }else if (sourceType instanceof PointerType) { + return makeBooleanCast(makePtrToIntCast(operand)); } throw new UnsupportedOperationException(String.format("Cannot cast %s to %s.", sourceType, booleanType)); } @@ -97,6 +99,10 @@ public IntLiteral parseValue(String text, IntegerType type) { return makeValue(new BigInteger(text), type); } + public IntLiteral makeValue(BigInteger value) { + return new IntLiteral(types.getArchType(), value); + } + public IntLiteral makeValue(long value, IntegerType type) { return makeValue(BigInteger.valueOf(value), type); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java index 0fee5afe20..9e5c01f19c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java @@ -4,6 +4,7 @@ import com.dat3m.dartagnan.expression.ExpressionKind; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.base.ExpressionBase; +import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.google.common.base.Preconditions; @@ -16,7 +17,7 @@ public class PointerAddExpr extends ExpressionBase { public PointerAddExpr(Expression base, Expression offset) { super((PointerType) base.getType()); - Preconditions.checkArgument(offset.getType().equals(TypeFactory.getInstance().getArchType())); + Preconditions.checkArgument(offset.getType() instanceof IntegerType); this.base = base; this.offset = offset; } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/cat/VisitorCat.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/cat/VisitorCat.java index 5c99029a99..5b53603b44 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/cat/VisitorCat.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/cat/VisitorCat.java @@ -30,6 +30,11 @@ import static com.dat3m.dartagnan.program.event.Tag.VISIBLE; import static com.dat3m.dartagnan.wmm.RelationNameRepository.ID; +import com.dat3m.dartagnan.parsers.CatParser.*; +import com.dat3m.dartagnan.wmm.definition.*; +import org.antlr.v4.runtime.*; +import java.util.*; + class VisitorCat extends CatBaseVisitor { private static final Logger logger = LogManager.getLogger(VisitorCat.class); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java index 452bae2f16..bbdaf7104f 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java @@ -302,7 +302,8 @@ public Object visitBranchEqual(AsmArmParser.BranchEqualContext ctx) { @Override public Object visitBranchNotEqual(AsmArmParser.BranchNotEqualContext ctx) { Label label = AsmUtils.getOrNewLabel(labelsDefined, ctx.Numbers().getText()); - Expression expr = expressions.makePtrCmp(comparator.left(), PointerCmpOp.NEQ, comparator.right()); + // todo remove forced ptr to int + Expression expr = expressions.makeIntCmpForced(comparator.left(), IntCmpOp.NEQ, comparator.right()); asmInstructions.add(EventFactory.newJump(expr, label)); return null; } @@ -390,10 +391,16 @@ public Object visitAsmMetadataEntries(AsmArmParser.AsmMetadataEntriesContext ctx @Override public Object visitValue(AsmArmParser.ValueContext ctx) { - checkState(expectedType instanceof IntegerType, "Expected type is not an integer type"); - String valueString = ctx.Numbers().getText(); - BigInteger value = new BigInteger(valueString); - return expressions.makeValue(value, (IntegerType) expectedType); + if (expectedType instanceof IntegerType) { + String valueString = ctx.Numbers().getText(); + BigInteger value = new BigInteger(valueString); + return expressions.makeValue(value, (IntegerType) expectedType); + }else if (expectedType instanceof PointerType){ + String valueString = ctx.Numbers().getText(); + BigInteger value = new BigInteger(valueString); + return expressions.makeIntToPtrCast(expressions.makeValue(value)); + } + throw new RuntimeException("Unexpected type " + expectedType + " visited"); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmRISCV.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmRISCV.java index 01b238abe3..111c62e8ff 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmRISCV.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmRISCV.java @@ -12,6 +12,7 @@ import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.integers.IntCmpOp; import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.parsers.AsmRISCVBaseVisitor; import com.dat3m.dartagnan.parsers.AsmRISCVParser; import com.dat3m.dartagnan.parsers.program.utils.AsmUtils; @@ -87,7 +88,7 @@ public Object visitLoad(AsmRISCVParser.LoadContext ctx) { Register address = (Register) ctx.register(1).accept(this); expectedType = address.getType(); Expression offset = (Expression) ctx.value().accept(this); - Expression newAddress = expressions.makeAdd(address,offset); + Expression newAddress = expressions.makePtrAdd(address,offset); asmInstructions.add(EventFactory.newLoad(register, newAddress)); return null; } @@ -143,7 +144,7 @@ public Object visitStore(AsmRISCVParser.StoreContext ctx) { Register address = (Register) ctx.register(1).accept(this); expectedType = address.getType(); Expression offset = (Expression) ctx.value().accept(this); - Expression newAddress = expressions.makeAdd(address,offset); + Expression newAddress = expressions.makePtrAdd(address,offset); asmInstructions.add(EventFactory.newStore(newAddress, value)); return null; } @@ -324,10 +325,16 @@ public Object visitRiscvFence(AsmRISCVParser.RiscvFenceContext ctx) { @Override public Object visitValue(AsmRISCVParser.ValueContext ctx) { - checkState(expectedType instanceof IntegerType, "Expected type is not an integer type"); + if (expectedType instanceof IntegerType) { String valueString = ctx.Numbers().getText(); BigInteger value = new BigInteger(valueString); return expressions.makeValue(value, (IntegerType) expectedType); + }else if (expectedType instanceof PointerType){ + String valueString = ctx.Numbers().getText(); + BigInteger value = new BigInteger(valueString); + return expressions.makeIntToPtrCast(expressions.makeValue(value)); } + throw new RuntimeException("Unexpected type " + expectedType + " visited"); +} } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index 83a27d6597..05c48e3aa2 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -111,7 +111,8 @@ public Object visitGlobalDeclaratorRegisterLocation(LitmusCParser.GlobalDeclarat if(object != null){ programBuilder.initRegEqConst(ctx.threadId().id, ctx.varName(0).getText(), object); } else { - programBuilder.initRegEqLocVal(ctx.threadId().id, ctx.varName(0).getText(), ctx.varName(1).getText(), archType); + programBuilder.initRegEqLocVal(ctx.threadId().id, ctx.varName(0).getText(), ctx.varName(1).getText(), pointerType); + // todo verify pointerType or intType } } return null; @@ -600,7 +601,7 @@ public Object visitNreAssignment(LitmusCParser.NreAssignmentContext ctx){ public Object visitNreRegDeclaration(LitmusCParser.NreRegDeclarationContext ctx){ Register register = programBuilder.getRegister(scope, ctx.varName().getText()); if(register == null){ - register = programBuilder.getOrNewRegister(scope, ctx.varName().getText(), archType); + register = programBuilder.getOrNewRegister(scope, ctx.varName().getText(), pointerType); //care if(ctx.re() != null){ returnRegister = register; ctx.re().accept(this); @@ -676,14 +677,14 @@ public Expression visitVarName(LitmusCParser.VarNameContext ctx){ } MemoryObject object = programBuilder.getMemoryObject(ctx.getText()); if(object != null){ - register = programBuilder.getOrNewRegister(scope, null, archType); + register = programBuilder.getOrNewRegister(scope, null, pointerType); programBuilder.addChild(currentThread, EventFactory.newLoadWithMo(register, object, C11.NONATOMIC)); return register; } - return programBuilder.getOrNewRegister(scope, ctx.getText(), archType); + return programBuilder.getOrNewRegister(scope, ctx.getText(), pointerType); } MemoryObject object = programBuilder.newMemoryObject(ctx.getText(), archSize); - Register register = programBuilder.getOrNewRegister(scope, null, archType); + Register register = programBuilder.getOrNewRegister(scope, null, pointerType); programBuilder.addChild(currentThread, EventFactory.newLoadWithMo(register, object, C11.NONATOMIC)); return register; } @@ -706,7 +707,7 @@ private Expression returnExpressionOrOne(LitmusCParser.ReContext ctx) { private Register getReturnRegister(boolean createOnNull){ Register register = returnRegister; if(register == null && createOnNull){ - return programBuilder.getOrNewRegister(scope, null, archType); + return programBuilder.getOrNewRegister(scope, null, pointerType); } returnRegister = null; return register; From 9b0883096aed225c32038b0b11818302f4b5d47b Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Thu, 6 Nov 2025 08:30:59 +0100 Subject: [PATCH 17/33] Litmus lkmm changes --- .../expression/ExpressionFactory.java | 26 +++++++++++++++++++ .../processing/compilation/VisitorLKMM.java | 17 ++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index e4292d1cdc..bc2420ad67 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -9,6 +9,7 @@ import com.dat3m.dartagnan.expression.pointer.*; import com.dat3m.dartagnan.expression.type.*; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; +import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.memory.MemoryObject; import com.dat3m.dartagnan.program.memory.ScopedPointer; import com.dat3m.dartagnan.program.memory.ScopedPointerVariable; @@ -498,6 +499,17 @@ public Expression makeEQ(Expression leftOperand, Expression rightOperand) { throw new UnsupportedOperationException("Equality not supported on type: " + type); } + public Expression makeEQforced(Expression leftOperand, Expression rightOperand) { + + if (leftOperand.getType() instanceof PointerType){ + return makeEQforced(makePtrToIntCast(leftOperand), rightOperand); + } + if (rightOperand.getType() instanceof PointerType){ + return makeEQforced(leftOperand, makePtrToIntCast(rightOperand)); + } + return makeEQ(leftOperand, rightOperand); + } + public Expression makeNEQ(Expression leftOperand, Expression rightOperand) { final Type type = leftOperand.getType(); if (type instanceof BooleanType) { @@ -515,6 +527,18 @@ public Expression makeNEQ(Expression leftOperand, Expression rightOperand) { throw new UnsupportedOperationException("Disequality not supported on type: " + type); } + public Expression makeNEQforced(Expression leftOperand, Expression rightOperand) { + + if (leftOperand.getType() instanceof PointerType){ + return makeNEQforced(makePtrToIntCast(leftOperand), rightOperand); + } + if (rightOperand.getType() instanceof PointerType){ + return makeNEQforced(leftOperand, makePtrToIntCast(rightOperand)); + } + return makeNEQ(leftOperand, rightOperand); + } + + public Expression makeUnary(ExpressionKind op, Expression expr) { if (op instanceof BoolUnaryOp boolOp) { return makeBoolUnary(boolOp, expr); @@ -553,4 +577,6 @@ public Expression makeCompare(Expression x, ExpressionKind cmpOp, Expression y) } throw new UnsupportedOperationException(String.format("Expression kind %s is no comparison operator.", cmpOp)); } + + } \ No newline at end of file diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorLKMM.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorLKMM.java index d858308a5f..5b14612997 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorLKMM.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorLKMM.java @@ -2,6 +2,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.ExpressionFactory; +import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.event.Event; @@ -70,13 +71,13 @@ public List visitLKMMCmpXchg(LKMMCmpXchg e) { newJump(havocRegister, success), // Cas failure branch loadFail, - newAssume(expressions.makeNEQ(dummy, cmp)), + newAssume(expressions.makeNEQforced(dummy, cmp)), newGoto(end), success, // CAS success branch mo.equals(Tag.Linux.MO_MB) ? newCoreMemoryBarrier() : null, loadSuccess = newRMWLoadWithMo(dummy, address, Tag.Linux.loadMO(mo)), - newAssume(expressions.makeEQ(dummy, cmp)), + newAssume(expressions.makeEQforced(dummy, cmp)), newRMWStoreWithMo(loadSuccess, address, e.getStoreValue(), Tag.Linux.storeMO(mo)), mo.equals(Tag.Linux.MO_MB) ? newCoreMemoryBarrier() : null, end, @@ -94,12 +95,12 @@ public List visitLKMMFetchOp(LKMMFetchOp e) { Event optionalMbBefore = mo.equals(Tag.Linux.MO_MB) ? newCoreMemoryBarrier() : null; Load load = newRMWLoadWithMo(dummy, address, Tag.Linux.loadMO(mo)); Event optionalMbAfter = mo.equals(Tag.Linux.MO_MB) ? newCoreMemoryBarrier() : null; - Expression storeValue = expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand()); + Expression storeValue = expressions.makeIntBinaryForced(dummy,e.getOperator(), e.getOperand()); return eventSequence( optionalMbBefore, load, - newRMWStoreWithMo(load, address, storeValue, Tag.Linux.storeMO(mo)), + newRMWStoreWithMo(load, address, dummy.getType() instanceof IntegerType ? storeValue: expressions.makeIntToPtrCast(storeValue), Tag.Linux.storeMO(mo)), newLocal(resultRegister, dummy), optionalMbAfter ); @@ -128,11 +129,11 @@ public List visitLKMMOpAndTest(LKMMOpAndTest e) { Register dummy = e.getFunction().newRegister(operand.getType()); Load load = newRMWLoadWithMo(dummy, address, Tag.Linux.MO_ONCE); Expression testResult = expressions.makeNot(expressions.makeBooleanCast(dummy)); - + Expression temp = expressions.makeIntBinaryForced(dummy, e.getOperator(), operand); return eventSequence( newCoreMemoryBarrier(), load, - newLocal(dummy, expressions.makeIntBinary(dummy, e.getOperator(), operand)), + newLocal(dummy, dummy.getType() instanceof IntegerType ? temp : expressions.makeIntToPtrCast(temp)), newRMWStoreWithMo(load, address, dummy, Tag.Linux.MO_ONCE), newLocal(resultRegister, expressions.makeCast(testResult, resultRegister.getType())), newCoreMemoryBarrier() @@ -149,11 +150,11 @@ public List visitLKMMOpReturn(LKMMOpReturn e) { Load load = newRMWLoadWithMo(dummy, address, Tag.Linux.loadMO(mo)); Event optionalMbBefore = mo.equals(Tag.Linux.MO_MB) ? newCoreMemoryBarrier() : null; Event optionalMbAfter = mo.equals(Tag.Linux.MO_MB) ? newCoreMemoryBarrier() : null; - + Expression temp = expressions.makeIntBinaryForced(dummy,e.getOperator(), e.getOperand()); return eventSequence( optionalMbBefore, load, - newLocal(dummy, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand())), + newLocal(dummy, resultRegister.getType() instanceof IntegerType ? temp: expressions.makeIntToPtrCast(temp) ), newRMWStoreWithMo(load, address, dummy, Tag.Linux.storeMO(mo)), newLocal(resultRegister, dummy), optionalMbAfter From 2fdfd3b8da32f35933aad632d388252ecd9facc3 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Thu, 6 Nov 2025 08:42:35 +0100 Subject: [PATCH 18/33] Litmus c11 fix --- .../dartagnan/program/processing/compilation/VisitorC11.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorC11.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorC11.java index 49f39097d5..88de5e37e7 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorC11.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorC11.java @@ -4,6 +4,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.type.BooleanType; +import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.event.Event; import com.dat3m.dartagnan.program.event.EventFactory; @@ -82,7 +83,8 @@ public List visitAtomicFetchOp(AtomicFetchOp e) { Register dummyReg = e.getFunction().newRegister(resultRegister.getType()); Load load = newRMWLoadWithMo(resultRegister, address, Tag.C11.loadMO(mo)); - Local localOp = newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())); + Expression temp = expressions.makeIntBinaryForced(resultRegister, e.getOperator(), e.getOperand()); + Local localOp = newLocal(dummyReg, dummyReg.getType() instanceof IntegerType ? temp : expressions.makeIntToPtrCast(temp)); RMWStore store = newRMWStoreWithMo(load, address, dummyReg, Tag.C11.storeMO(mo)); return tagList(e, eventSequence( From 2825d0eb3865abdb977e4d688cf7627cbb7ed17a Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Thu, 6 Nov 2025 09:26:29 +0100 Subject: [PATCH 19/33] Added pointer concat and extract --- .../expression/ExpressionVisitor.java | 3 ++ .../expression/pointer/PtrConcat.java | 34 +++++++++++++++ .../expression/pointer/PtrExtract.java | 43 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrConcat.java create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrExtract.java diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index 9023cb0310..f7f27e4465 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -63,6 +63,8 @@ public interface ExpressionVisitor { default TRet visitPtrToIntCastExpression(PtrToIntCast expr) { return visitCastExpression(expr); } default TRet visitPtrCmpExpression(PtrCmpExpr expr) { return visitBinaryExpression(expr); } default TRet visitNullLiteral(NullLiteral lit) { return visitLeafExpression(lit); } + default TRet visitPtrConcat(PtrConcat expr){ return visitExpression(expr); }; + default TRet visitPtrExtract(PtrExtract expr){ return visitUnaryExpression(expr); }; // =================================== Generic =================================== default TRet visitITEExpression(ITEExpr expr) { return visitExpression(expr); } @@ -80,4 +82,5 @@ private static UnsupportedOperationException unsupported(Expression expr, Class< expr.getClass().getSimpleName(), clazz.getSimpleName()); return new UnsupportedOperationException(error); } + } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrConcat.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrConcat.java new file mode 100644 index 0000000000..d291b88801 --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrConcat.java @@ -0,0 +1,34 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.Expression; +import com.dat3m.dartagnan.expression.ExpressionKind; +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.NaryExpressionBase; +import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; +import com.dat3m.dartagnan.expression.type.TypeFactory; +import com.dat3m.dartagnan.expression.utils.ExpressionHelper; +import com.google.common.collect.ImmutableList; + +import java.util.List; + +public class PtrConcat extends NaryExpressionBase { + + public PtrConcat(List operands) { + super(getConcatType(operands), ExpressionKind.Other.BV_CONCAT, ImmutableList.copyOf(operands)); + } + + private static PointerType getConcatType(List operands) { + int size = 0; + for (Expression op : operands) { + ExpressionHelper.checkExpectedType(op, PointerType.class); + size += ((IntegerType)op.getType()).getBitWidth(); + } + return TypeFactory.getInstance().getPointerType(size); + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitPtrConcat(this); + } +} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrExtract.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrExtract.java new file mode 100644 index 0000000000..1b8baf8789 --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrExtract.java @@ -0,0 +1,43 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.Expression; +import com.dat3m.dartagnan.expression.ExpressionKind; +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.UnaryExpressionBase; +import com.dat3m.dartagnan.expression.type.IntegerType; +import com.dat3m.dartagnan.expression.type.PointerType; +import com.dat3m.dartagnan.expression.type.TypeFactory; +import com.dat3m.dartagnan.expression.utils.ExpressionHelper; +import com.google.common.base.Preconditions; + +public class PtrExtract extends UnaryExpressionBase { + + private final int lowBit; + private final int highBit; + + public PtrExtract(Expression operand, int lowBit, int highBit) { + super(TypeFactory.getInstance().getPointerType(highBit - lowBit + 1), + ExpressionKind.Other.BV_EXTRACT, + operand); + ExpressionHelper.checkExpectedType(operand, PointerType.class); + int originalWidth = ((IntegerType)operand.getType()).getBitWidth(); + Preconditions.checkArgument(0 <= lowBit && lowBit <= highBit && highBit < originalWidth); + this.lowBit = lowBit; + this.highBit = highBit; + } + + public int getLowBit() { return lowBit; } + public int getHighBit() { return highBit; } + + public boolean isExtractingLowBits() { + return lowBit == 0; + } + public boolean isExtractingHighBits() { + return operand.getType() instanceof IntegerType t && highBit + 1 == t.getBitWidth(); + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitPtrExtract(this); + } +} From 63a4678a59c7d7f1c9b7433607c261455e0367a9 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 10 Nov 2025 12:33:18 +0100 Subject: [PATCH 20/33] Added pointer recognition to ops VisitorOpsConstant --- .../parsers/program/visitors/spirv/VisitorOpsConstant.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsConstant.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsConstant.java index 465e09b29f..57d199d67d 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsConstant.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsConstant.java @@ -143,6 +143,9 @@ private Expression getConstantNullExpression(String typeId, Type type) { if (type instanceof IntegerType iType) { return expressions.makeZero(iType); } + if (type instanceof PointerType pType) { + return expressions.makeNullLiteral(pType); + } throw new ParsingException("Unsupported NULL constant type '%s'", typeId); } From a2a11d6a09a4672080d0ee23030aac956e3b38a9 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Mon, 10 Nov 2025 18:04:25 +0100 Subject: [PATCH 21/33] -- --- .../com/dat3m/dartagnan/program/processing/Intrinsics.java | 1 - .../dartagnan/program/processing/ProcessingManager.java | 2 ++ .../java/com/dat3m/dartagnan/utils/printer/Printer.java | 6 +++--- .../java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index 93f4912733..c95ea7c3d8 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -712,7 +712,6 @@ private List inlinePthreadKeyCreate(FunctionCall call) { final Expression destructor = call.getArguments().get(1); final Program program = call.getFunction().getProgram(); final long threadCount = program.getThreads().size(); - final int pointerBytes = types.getMemorySizeInBytes(types.getPointerType()); final Register storageAddressRegister = call.getFunction().newRegister(types.getArchType()); final Expression size = expressions.makeValue((threadCount + 1) * pointerBytes, types.getArchType()); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java index 0ea7d57060..63669efed0 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java @@ -130,7 +130,9 @@ private ProcessingManager(Configuration config) throws InvalidConfigurationExcep simplifyFunction ), Target.FUNCTIONS, true ), + ThreadCreation.fromConfig(config), + DebugPrint.withHeader("--------Debug--------", Printer.Mode.ALL, config), ResolveNonDetChoices.newInstance(), reduceSymmetry ? SymmetryReduction.fromConfig(config) : null, intrinsics.lateInliningPass(), diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/utils/printer/Printer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/utils/printer/Printer.java index d653108b46..c18dac55c3 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/utils/printer/Printer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/utils/printer/Printer.java @@ -41,7 +41,7 @@ public Printer setMode(Mode mode) { @Option(name = OptionNames.PRINTER_SHOW_INIT_THREADS, description = "Print init threads (default: false)", secure = true) - private boolean showInitThreads = false; + private boolean showInitThreads = true; @Option(name = OptionNames.PRINTER_SHOW_ANNOTATIONS, description = "Print annotation events (default: true)", @@ -56,12 +56,12 @@ public Printer setMode(Mode mode) { @Option(name = OptionNames.PRINTER_SHOW_PROGRAM_CONSTANTS, description = "Print non-deterministic program constants (default: false)", secure = true) - private boolean showProgramConstants = false; + private boolean showProgramConstants = true; @Option(name = OptionNames.PRINTER_SHOW_SPECIFICATION, description = "Print program specification and filter (default: false)", secure = true) - private boolean showSpecification = false; + private boolean showSpecification = true; // ================================================================================= diff --git a/dartagnan/src/test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java b/dartagnan/src/test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java index 263d1d3c43..8a56445cee 100644 --- a/dartagnan/src/test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java +++ b/dartagnan/src/test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java @@ -42,7 +42,7 @@ protected Provider getBoundProvider() { @Override protected long getTimeout() { - return 10000; + return 1000000; } @Override From 8773284b8f110d34cacce20d9644ab1076f68d9f Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Tue, 11 Nov 2025 21:19:08 +0100 Subject: [PATCH 22/33] added encoding and printing of pointer tearing --- .../dartagnan/encoding/ExpressionEncoder.java | 60 +++++++++++++++---- .../expression/ExpressionFactory.java | 9 ++- .../expression/ExpressionPrinter.java | 10 ++++ .../expression/pointer/PtrConcat.java | 3 +- .../expression/pointer/PtrExtract.java | 5 +- .../processing/ExprTransformer.java | 16 +++-- .../expression/type/PointerType.java | 4 +- .../expression/type/TypeFactory.java | 4 +- .../program/processing/ProcessingManager.java | 2 +- .../dartagnan/program/processing/Tearing.java | 7 ++- .../program/processing/ThreadCreation.java | 14 ++--- 11 files changed, 97 insertions(+), 37 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index f059d47cc3..b45e5eb88a 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -551,7 +551,7 @@ public TypedFormula visitIntCmpExpression(IntCmpExp final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); final BitvectorFormula innerBv = (BitvectorFormula) inner.formula(); final int targetBitWidth = expr.getTargetType().getBitWidth(); - final int sourceBitWidth = types.getArchType().getBitWidth(); + final int sourceBitWidth = expr.getSourceType().getBitWidth(); assert (sourceBitWidth == bvmgr.getLength(innerBv)); enc = bvmgr.extract(innerBv, targetBitWidth - 1, 0); } @@ -583,16 +583,16 @@ public TypedFormula visitPtrCmpExpression(PtrCmpExp }; return new TypedFormula<>(types.getBooleanType(), result); }else{ - final var left = (TypedFormula) encodePointerExpr(expr.getLeft()); - final var right = (TypedFormula) encodePointerExpr(expr.getRight()); + final BitvectorFormula left = (BitvectorFormula) encodePointerExpr(expr.getLeft()).formula(); + final BitvectorFormula right = (BitvectorFormula) encodePointerExpr(expr.getRight()).formula(); final BitvectorFormulaManager bvgr = bitvectorFormulaManager(); final BooleanFormula result = switch (expr.getKind()) { - case EQ -> bvgr.equal(left.formula(), right.formula()); - case NEQ -> bmgr.not(fmgr.equal(left.formula(), right.formula())); - case LT -> bvgr.lessThan(left.formula(), right.formula(),false); - case LTE -> bvgr.lessOrEquals(left.formula(), right.formula(),false); - case GT -> bvgr.greaterThan(left.formula(), right.formula(),false); - case GTE -> bvgr.greaterOrEquals(left.formula(), right.formula(),false); + case EQ -> bvgr.equal(left, right); + case NEQ -> bmgr.not(fmgr.equal(left, right)); + case LT -> bvgr.lessThan(left, right,false); + case LTE -> bvgr.lessOrEquals(left, right,false); + case GT -> bvgr.greaterThan(left, right,false); + case GTE -> bvgr.greaterOrEquals(left, right,false); }; return new TypedFormula<>(types.getBooleanType(), result); } @@ -607,9 +607,47 @@ public TypedFormula visitPtrCmpExpression(PtrCmpExp return new TypedFormula<>(lit.getType(), zero); } + @Override + public TypedFormula visitPtrExtract(PtrExtract expr) { + final Formula operand = encodePointerExpr(expr.getOperand()).formula(); + final Formula enc; + if (context.useIntegers) { + final IntegerFormulaManager imgr = integerFormulaManager(); + final IntegerFormula highBitValue = imgr.makeNumber(BigInteger.TWO.pow(expr.getHighBit() + 1)); + final IntegerFormula lowBitValue = imgr.makeNumber(BigInteger.TWO.pow(expr.getLowBit())); + final IntegerFormula op = (IntegerFormula) operand; + final IntegerFormula extracted = expr.isExtractingHighBits() ? op : imgr.modulo(op, highBitValue); + enc = expr.isExtractingLowBits() ? extracted : imgr.divide(extracted, lowBitValue); + } else { + final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); + enc = bvmgr.extract((BitvectorFormula) operand, expr.getHighBit(), expr.getLowBit()); + } + return new TypedFormula<>(expr.getType(), enc); + } - - + @Override + public TypedFormula visitPtrConcat(PtrConcat expr) { + Preconditions.checkArgument(!expr.getOperands().isEmpty()); + final List> operands = expr.getOperands().stream() + .map(this::encodePointerExpr) + .toList(); + Formula enc = operands.get(0).formula(); + if (context.useIntegers) { + final IntegerFormulaManager imgr = fmgr.getIntegerFormulaManager(); + int offset = operands.get(0).type().getBitWidth(); + for (TypedFormula op : operands.subList(1, operands.size())) { + final IntegerFormula offsetValue = imgr.makeNumber(BigInteger.TWO.pow(offset - 1)); + enc = imgr.add((IntegerFormula) enc, imgr.multiply((IntegerFormula) op.formula(), offsetValue)); + offset += op.type().getBitWidth(); + } + } else { + final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); + for (TypedFormula op : operands.subList(1, operands.size())) { + enc = bvmgr.concat((BitvectorFormula) op.formula(), (BitvectorFormula) enc); + } + } + return new TypedFormula<>(expr.getType(), enc); + } // ==================================================================================== // Aggregates diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index bc2420ad67..d5d0a0af19 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -414,7 +414,6 @@ public Expression makeIntToPtrCast(Expression operand) { } - public Expression makeNullLiteral(PointerType pointerType) { return new NullLiteral(pointerType); } @@ -427,6 +426,14 @@ public Expression makePtrCmp(Expression left, PointerCmpOp op, Expression right) return new PtrCmpExpr(types.getBooleanType(), left, op, right); } + public Expression makePtrExtract(Expression operand, int lowBit, int highBit) { + return new PtrExtract(operand, lowBit, highBit); + } + + public Expression makePtrConcat(List operands) { + return new PtrConcat(operands); + } + // ----------------------------------------------------------------------------------------------------------------- // Misc diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java index e267544d20..9e0fcc77e8 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java @@ -140,6 +140,16 @@ public String visitPointerAddExpression(PointerAddExpr expr) { return String.format("%s + %s", visit(expr.getBase()), visit(expr.getOffset())); } + @Override + public String visitPtrConcat(PtrConcat expr) { + return Lists.reverse(expr.getOperands()).stream().map(this::visit).collect(Collectors.joining("::")); + } + + @Override + public String visitPtrExtract(PtrExtract expr) { + return String.format("%s[%d..%d]", expr.getOperand().accept(this), expr.getLowBit(), expr.getHighBit()); + } + @Override public String visitITEExpression(ITEExpr expr) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrConcat.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrConcat.java index d291b88801..abe239c1b5 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrConcat.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrConcat.java @@ -4,7 +4,6 @@ import com.dat3m.dartagnan.expression.ExpressionKind; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.base.NaryExpressionBase; -import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; @@ -22,7 +21,7 @@ private static PointerType getConcatType(List operands) { int size = 0; for (Expression op : operands) { ExpressionHelper.checkExpectedType(op, PointerType.class); - size += ((IntegerType)op.getType()).getBitWidth(); + size += ((PointerType)op.getType()).getBitWidth(); } return TypeFactory.getInstance().getPointerType(size); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrExtract.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrExtract.java index 1b8baf8789..8f74410104 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrExtract.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrExtract.java @@ -4,7 +4,6 @@ import com.dat3m.dartagnan.expression.ExpressionKind; import com.dat3m.dartagnan.expression.ExpressionVisitor; import com.dat3m.dartagnan.expression.base.UnaryExpressionBase; -import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.type.TypeFactory; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; @@ -20,7 +19,7 @@ public PtrExtract(Expression operand, int lowBit, int highBit) { ExpressionKind.Other.BV_EXTRACT, operand); ExpressionHelper.checkExpectedType(operand, PointerType.class); - int originalWidth = ((IntegerType)operand.getType()).getBitWidth(); + int originalWidth = ((PointerType)operand.getType()).getBitWidth(); Preconditions.checkArgument(0 <= lowBit && lowBit <= highBit && highBit < originalWidth); this.lowBit = lowBit; this.highBit = highBit; @@ -33,7 +32,7 @@ public boolean isExtractingLowBits() { return lowBit == 0; } public boolean isExtractingHighBits() { - return operand.getType() instanceof IntegerType t && highBit + 1 == t.getBitWidth(); + return operand.getType() instanceof PointerType t && highBit + 1 == t.getBitWidth(); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java index 9549e85cbd..b4051dab9b 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java @@ -17,10 +17,8 @@ import com.dat3m.dartagnan.expression.misc.GEPExpr; import com.dat3m.dartagnan.expression.misc.ITEExpr; import com.dat3m.dartagnan.expression.type.TypeFactory; -import com.dat3m.dartagnan.expression.pointer.IntToPtrCast; -import com.dat3m.dartagnan.expression.pointer.PointerAddExpr; -import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; -import com.dat3m.dartagnan.expression.pointer.PtrToIntCast; +import com.dat3m.dartagnan.expression.pointer.*; + import java.util.ArrayList; @@ -149,6 +147,16 @@ public Expression visitIntToPtrCastExpression(IntToPtrCast expr) { return expressions.makeIntToPtrCast(expr.getOperand().accept(this)); } + @Override + public Expression visitPtrConcat(PtrConcat expr) { + return expressions.makePtrConcat(expr.getOperands().stream().map(e -> e.accept(this)).toList()); + } + + @Override + public Expression visitPtrExtract(PtrExtract expr) { + return expressions.makePtrExtract(expr.getOperand().accept(this), expr.getLowBit(), expr.getHighBit()); + } + @Override public Expression visitGEPExpression(GEPExpr gep) { Expression base = gep.getBase().accept(this); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java index 667b1bb3fb..4adadd85a5 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -12,7 +12,6 @@ public class PointerType implements Type { this.bitWidth = bitWidth; } - @Override public String toString() { return "ptr_"+ bitWidth; @@ -24,4 +23,7 @@ public boolean equals(Object obj) { && other.bitWidth == this.bitWidth; } + public int getBitWidth() { + return bitWidth; + } } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java index 2a0ec6cfc8..547957e06d 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java @@ -166,8 +166,8 @@ private int getMemorySizeInBytes(Type type, boolean padded) { if (type instanceof IntegerType integerType) { return IntMath.divide(integerType.getBitWidth(), 8, RoundingMode.CEILING); } - if (type instanceof PointerType) { - return getMemorySizeInBytes(getArchType()); + if (type instanceof PointerType pointerTypp) { + return IntMath.divide(pointerTypp.getBitWidth(), 8, RoundingMode.CEILING); } if (type instanceof FloatType floatType) { return IntMath.divide(floatType.getBitWidth(), 8, RoundingMode.CEILING); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java index 63669efed0..3e0816254a 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java @@ -132,7 +132,7 @@ private ProcessingManager(Configuration config) throws InvalidConfigurationExcep ), ThreadCreation.fromConfig(config), - DebugPrint.withHeader("--------Debug--------", Printer.Mode.ALL, config), + //DebugPrint.withHeader("--------Debug--------", Printer.Mode.ALL, config), ResolveNonDetChoices.newInstance(), reduceSymmetry ? SymmetryReduction.fromConfig(config) : null, intrinsics.lateInliningPass(), diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java index ca4491fcc7..bb1c73cec2 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java @@ -177,6 +177,7 @@ private List createTransaction(Load load, List offsets) { final PointerType addressType = checkPointerType(load.getAddress().getType(), "Non-Pointer address in '%s'", load); checkType(load.getAccessType(), "Non-integer or Pointer mixed-size access in '%s'", load); + final Register resultRegister = load.getResultRegister(); final Function function = load.getFunction(); final Register addressRegister = toRegister(load.getAddress(), function, replacement); final List smallerRegisters = new ArrayList<>(); @@ -184,7 +185,7 @@ private List createTransaction(Load load, List offsets) { int start = i < 0 ? 0 : offsets.get(i); int end = i + 1 < offsets.size() ? offsets.get(i + 1) : bytes; assert start < end; - smallerRegisters.add(newRegister(function, types.getIntegerType(8 * (end - start)))); + smallerRegisters.add(newRegister(function, resultRegister.getType() instanceof PointerType ?types.getPointerType(8 * (end - start)) :types.getIntegerType(8 * (end - start)))); } assert bytes == smallerRegisters.stream().mapToInt(t -> types.getMemorySizeInBytes(t.getType())).sum(); final InstructionBoundary begin = load.hasTag(Tag.NO_INSTRUCTION) ? null : EventFactory.newInstructionBegin(); @@ -206,7 +207,7 @@ private List createTransaction(Load load, List offsets) { final Event end = EventFactory.newInstructionEnd(begin); replacement.add(end); } - final Expression combination = expressions.makeIntConcat(smallerRegisters); + final Expression combination = resultRegister.getType() instanceof PointerType ? expressions.makePtrConcat(smallerRegisters): expressions.makeIntConcat(smallerRegisters); final Event computeResult = EventFactory.newLocal(load.getResultRegister(), combination); replacement.add(computeResult); return replacement; @@ -233,7 +234,7 @@ private List createTransaction(Store store, List offsets, Map createThreads(Entrypoint.Simple entrypoint) { final List replacement = eventSequence( newReleaseStore(spawnedThread.comAddress(), threadState(ALIVE | JOINABLE)), createEvent, - newLocal(tidRegister, new TIdExpr(archType, spawnedThread.thread())) + newLocal(tidRegister, new TIdExpr((IntegerType) tidRegister.getType(), spawnedThread.thread())) ); IRHelper.replaceWithMetadata(create, replacement); @@ -345,7 +345,7 @@ private void resolvePthreadSelf(Program program) { final Expression tidExpr = new TIdExpr(archType, call.getThread()); final Local tidAssignment = newLocal(resultRegister, expressions.makeIntToPtrCast(tidExpr)); IRHelper.replaceWithMetadata(call, tidAssignment); - }else { + }else{ final Expression tidExpr = new TIdExpr((IntegerType) regType, call.getThread()); final Local tidAssignment = newLocal(resultRegister, tidExpr); IRHelper.replaceWithMetadata(call, tidAssignment); @@ -435,7 +435,6 @@ private ThreadData createLLVMThreadFromFunction(Function function, int tid, Thre return new ThreadData(thread, null); } - private void replaceThreadLocalsWithStackalloc(Memory memory, Thread thread) { final TypeFactory types = TypeFactory.getInstance(); final ExpressionFactory exprs = ExpressionFactory.getInstance(); @@ -443,11 +442,8 @@ private void replaceThreadLocalsWithStackalloc(Memory memory, Thread thread) { // Translate thread-local memory object to local stack allocation Map toLocalRegister = new HashMap<>(); for (MemoryObject memoryObject : memory.getObjects()) { - if (!memoryObject.isThreadLocal()) { - continue; - } + if (!memoryObject.isThreadLocal()) {continue;} Preconditions.checkState(memoryObject.hasKnownSize()); - // Compute type of memory object based on initial values final List contentTypes = new ArrayList<>(); final List offsets = new ArrayList<>(); @@ -460,7 +456,7 @@ private void replaceThreadLocalsWithStackalloc(Memory memory, Thread thread) { // Allocate single object of memory type final Register reg = thread.newUniqueRegister("__threadLocal_" + memoryObject, types.getPointerType()); final Event localAlloc = EventFactory.newAlloc( - reg, memoryType, expressions.makeOne(types.getArchType()), + reg, memoryType, expressions.makeOne(archType), false, true ); @@ -468,7 +464,7 @@ private void replaceThreadLocalsWithStackalloc(Memory memory, Thread thread) { final List initialization = new ArrayList<>(); for (Integer initOffset : memoryObject.getInitializedFields()) { initialization.add(EventFactory.newStore( - exprs.makePtrAdd(reg, exprs.makeValue(initOffset, types.getArchType())), + exprs.makePtrAdd(reg, exprs.makeValue(initOffset, archType)), memoryObject.getInitialValue(initOffset) )); } From 2e5614072ac565ae2fa9eb42cad466f515f99bfc Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Wed, 12 Nov 2025 19:21:13 +0100 Subject: [PATCH 23/33] fixed test change --- .../test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dartagnan/src/test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java b/dartagnan/src/test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java index 8a56445cee..263d1d3c43 100644 --- a/dartagnan/src/test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java +++ b/dartagnan/src/test/java/com/dat3m/dartagnan/llvm/MiscellaneousTest.java @@ -42,7 +42,7 @@ protected Provider getBoundProvider() { @Override protected long getTimeout() { - return 1000000; + return 10000; } @Override From 7dcac060f1aef9e8ab92cadd332bbe41ed1eb7fb Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Sun, 16 Nov 2025 20:08:17 +0100 Subject: [PATCH 24/33] fixed more pointer compatibility problems --- .../dartagnan/encoding/ExpressionEncoder.java | 12 +++++++----- .../expression/pointer/PointerCmpOp.java | 10 ++++++++++ .../expression/pointer/PtrToIntCast.java | 9 +++++++++ .../expression/type/ScopedPointerType.java | 2 +- .../parsers/program/utils/ProgramBuilder.java | 2 +- .../visitors/spirv/VisitorOpsAtomic.java | 2 +- .../dartagnan/program/event/EventFactory.java | 1 - .../program/processing/Inlining.java | 4 ---- .../program/processing/MemToReg.java | 3 ++- .../processing/NaiveLoopBoundAnnotation.java | 4 ++++ .../processing/RemoveDeadCondJumps.java | 4 ++++ .../SparseConditionalConstantPropagation.java | 6 ++++-- .../dartagnan/program/processing/Tearing.java | 11 ++++++++--- .../program/processing/ThreadCreation.java | 2 +- .../processing/compilation/VisitorArm8.java | 18 +++++++++--------- .../processing/compilation/VisitorPower.java | 19 ++++++++++--------- .../processing/compilation/VisitorRISCV.java | 18 +++++++++--------- .../processing/compilation/VisitorTso.java | 10 +++++++++- .../transformers/MemoryTransformer.java | 4 +--- 19 files changed, 90 insertions(+), 51 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index b45e5eb88a..d4916eae04 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -539,13 +539,15 @@ public TypedFormula visitIntCmpExpression(IntCmpExp @Override public TypedFormula visitPtrToIntCastExpression(PtrToIntCast expr) { - // Todo: add support for extension final TypedFormula inner = encodePointerExpr(expr.getOperand()); final Formula enc; if (context.useIntegers) { + if (expr.isExtension()) { + enc = inner.formula(); + }else { final BigInteger highValue = BigInteger.TWO.pow(expr.getType().getBitWidth()); final IntegerFormulaManager imgr = integerFormulaManager(); - enc = imgr.modulo((IntegerFormula) inner.formula(), imgr.makeNumber(highValue)); + enc = imgr.modulo((IntegerFormula) inner.formula(), imgr.makeNumber(highValue));} } else { assert inner.formula() instanceof BitvectorFormula; final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); @@ -553,15 +555,15 @@ public TypedFormula visitIntCmpExpression(IntCmpExp final int targetBitWidth = expr.getTargetType().getBitWidth(); final int sourceBitWidth = expr.getSourceType().getBitWidth(); assert (sourceBitWidth == bvmgr.getLength(innerBv)); - enc = bvmgr.extract(innerBv, targetBitWidth - 1, 0); + enc = expr.isExtension() + ? bvmgr.extend(innerBv, targetBitWidth - sourceBitWidth, false) + : bvmgr.extract(innerBv, targetBitWidth - 1, 0); } return new TypedFormula<>(expr.getType(), enc); - } @Override public TypedFormula visitIntToPtrCastExpression(IntToPtrCast expr) { - final TypedFormula address = encodeIntegerExpr(expr.getOperand()); return new TypedFormula<>(expr.getType(), address.formula()); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java index 0e16c29e54..a0c164fbb4 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java @@ -21,5 +21,15 @@ public String getSymbol() { case LT -> "<"; }; } + public PointerCmpOp inverted() { + return switch (this) { + case EQ -> NEQ; + case NEQ -> EQ; + case GTE -> LT; + case LTE -> GT; + case GT -> LTE; + case LT -> GTE; + }; + } } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java index 82a370ef63..5b9feebd5f 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrToIntCast.java @@ -6,6 +6,7 @@ import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; +import scala.concurrent.impl.FutureConvertersImpl; public final class PtrToIntCast extends CastExpressionBase { @@ -14,6 +15,14 @@ public PtrToIntCast(IntegerType integerType, Expression operand) { ExpressionHelper.checkExpectedType(operand, PointerType.class); } + public boolean isExtension() { + return isExtension(getSourceType(), getTargetType()); + } + + private static boolean isExtension(PointerType sourceType, IntegerType targetType) { + return sourceType.getBitWidth() < targetType.getBitWidth(); + } + @Override public T accept(ExpressionVisitor visitor) { return visitor.visitPtrToIntCastExpression(this); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/ScopedPointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/ScopedPointerType.java index eac26595d5..780d8c430e 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/ScopedPointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/ScopedPointerType.java @@ -6,7 +6,7 @@ public class ScopedPointerType extends PointerType { - private static final int ARCH_SIZE = TypeFactory.getInstance().getArchType().getBitWidth(); + private static final int ARCH_SIZE = TypeFactory.getInstance().getPointerType().getBitWidth(); private final String scopeId; private final Type pointedType; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/utils/ProgramBuilder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/utils/ProgramBuilder.java index 292dbd995c..518c1d7243 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/utils/ProgramBuilder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/utils/ProgramBuilder.java @@ -270,7 +270,7 @@ public Register getRegister(int fid, String name){ } public Register getOrNewRegister(int fid, String name) { - return getOrNewRegister(fid, name, types.getArchType()); + return getOrNewRegister(fid, name, types.getPointerType()); } public Register getOrNewRegister(int fid, String name, Type type) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsAtomic.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsAtomic.java index 0000843193..dd7cce41d0 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsAtomic.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/spirv/VisitorOpsAtomic.java @@ -145,7 +145,7 @@ private Event visitOpAtomicExtremum( Set tags = getMemorySemanticsTags(tagsCtx.getText()); tags.add(builder.getPointerStorageClass(ptrCtx.getText())); if (!(ptr.getType() instanceof PointerType) || !(value.getType() instanceof IntegerType)) { - throw new ParsingException("Unexpected type at '%s' or '%s', expected pointer and integer but received '%s' and '%s'", + throw new ParsingException("Unexpected type at '%s' or '%s', expected pointer or integer but received '%s' and '%s'", ptrCtx.getText(), valCtx.getText(), ptr.getType(), value.getType()); } SpirvRmwExtremum event = newSpirvRmwExtremum(register, ptr, kind, value, scope, tags); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/EventFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/EventFactory.java index ef062b9a52..8ed55c8ac7 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/EventFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/event/EventFactory.java @@ -165,7 +165,6 @@ public static NamedBarrier newNamedBarrier(String name, String instanceId, Strin public static Init newInit(MemoryObject base, int offset) { //TODO: We simplify here because virtual aliasing currently fails when pointer arithmetic is involved // meaning that and are treated differently. - // final Expression address = expressions.makePtrAdd(base, expressions.makeValue(offset, types.getArchType())); final Init init = new Init(base, offset, address); init.addTags(base.getFeatureTags()); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java index 7f89b95a3b..e0b7ed86ca 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java @@ -33,8 +33,6 @@ import static com.dat3m.dartagnan.program.event.EventFactory.*; import static com.google.common.base.Verify.verify; - - @Options public class Inlining implements ProgramProcessor { @@ -44,8 +42,6 @@ public class Inlining implements ProgramProcessor { private int bound = 1; private static final ExpressionFactory expressions = ExpressionFactory.getInstance(); - - private Inlining() {} public static Inlining fromConfig(Configuration config) throws InvalidConfigurationException { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/MemToReg.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/MemToReg.java index 8a54156b58..4703832749 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/MemToReg.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/MemToReg.java @@ -42,7 +42,8 @@ public class MemToReg implements FunctionProcessor { private static final TypeFactory types = TypeFactory.getInstance(); private static final ExpressionFactory expressions = ExpressionFactory.getInstance(); - private MemToReg() {} + private MemToReg() { + } public static MemToReg fromConfig(Configuration config) throws InvalidConfigurationException { return new MemToReg(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveLoopBoundAnnotation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveLoopBoundAnnotation.java index 703ebeb7f8..3d5b9cee85 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveLoopBoundAnnotation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveLoopBoundAnnotation.java @@ -4,6 +4,9 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.ITEExpr; +import com.dat3m.dartagnan.expression.pointer.NullLiteral; +import com.dat3m.dartagnan.expression.pointer.PointerCmpOp; +import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.analysis.DominatorAnalysis; @@ -108,6 +111,7 @@ public void run(Function function) { continue; } final Expression negatedGuard = jump.getGuard(); + // Now we check for the guard shape "r != 0" (i.e., we exit when r == 0 due to the inversion). if (!(negatedGuard instanceof IntCmpExpr cond && cond.getLeft() instanceof Register r diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/RemoveDeadCondJumps.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/RemoveDeadCondJumps.java index 5da9e12c4e..f1ba75eb32 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/RemoveDeadCondJumps.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/RemoveDeadCondJumps.java @@ -2,6 +2,7 @@ import com.dat3m.dartagnan.expression.booleans.BoolUnaryExpr; import com.dat3m.dartagnan.expression.integers.IntCmpExpr; +import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.event.Event; import com.dat3m.dartagnan.program.event.Tag; @@ -125,6 +126,9 @@ private boolean mutuallyExclusiveIfs(CondJump jump, Event e) { if (jump.getGuard() instanceof IntCmpExpr a1 && other.getGuard() instanceof IntCmpExpr a2) { return a1.getKind().inverted() == a2.getKind() && a1.getLeft().equals(a2.getLeft()) && a1.getRight().equals(a2.getRight()); } + if (jump.getGuard() instanceof PtrCmpExpr a1 && other.getGuard() instanceof PtrCmpExpr a2) { + return a1.getKind().inverted() == a2.getKind() && a1.getLeft().equals(a2.getLeft()) && a1.getRight().equals(a2.getRight()); + } return false; } } \ No newline at end of file diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/SparseConditionalConstantPropagation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/SparseConditionalConstantPropagation.java index 4c170c73d7..776f9af7de 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/SparseConditionalConstantPropagation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/SparseConditionalConstantPropagation.java @@ -3,7 +3,9 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.booleans.BoolLiteral; import com.dat3m.dartagnan.expression.integers.IntLiteral; +import com.dat3m.dartagnan.expression.pointer.NullLiteral; import com.dat3m.dartagnan.expression.processing.ExprSimplifier; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.IRHelper; import com.dat3m.dartagnan.program.Register; @@ -70,8 +72,8 @@ public void run(Function func) { return; } final Predicate checkDoPropagate = propagateCopyAssignments - ? (expr -> expr instanceof MemoryObject || expr instanceof IntLiteral || expr instanceof BoolLiteral || expr instanceof Register) - : (expr -> expr instanceof MemoryObject || expr instanceof IntLiteral || expr instanceof BoolLiteral); + ? (expr -> expr instanceof MemoryObject || expr instanceof NullLiteral || expr instanceof IntLiteral || expr instanceof BoolLiteral || expr instanceof Register) + : (expr -> expr instanceof MemoryObject || expr instanceof NullLiteral || expr instanceof IntLiteral || expr instanceof BoolLiteral); // todo check nullLiteral Set reachableEvents = new HashSet<>(); Map> inflowMap = new HashMap<>(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java index bb1c73cec2..062a364422 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java @@ -185,7 +185,9 @@ private List createTransaction(Load load, List offsets) { int start = i < 0 ? 0 : offsets.get(i); int end = i + 1 < offsets.size() ? offsets.get(i + 1) : bytes; assert start < end; - smallerRegisters.add(newRegister(function, resultRegister.getType() instanceof PointerType ?types.getPointerType(8 * (end - start)) :types.getIntegerType(8 * (end - start)))); + smallerRegisters.add(newRegister(function, resultRegister.getType() instanceof PointerType + ?types.getPointerType(8 * (end - start)) + :types.getIntegerType(8 * (end - start)))); } assert bytes == smallerRegisters.stream().mapToInt(t -> types.getMemorySizeInBytes(t.getType())).sum(); final InstructionBoundary begin = load.hasTag(Tag.NO_INSTRUCTION) ? null : EventFactory.newInstructionBegin(); @@ -207,7 +209,8 @@ private List createTransaction(Load load, List offsets) { final Event end = EventFactory.newInstructionEnd(begin); replacement.add(end); } - final Expression combination = resultRegister.getType() instanceof PointerType ? expressions.makePtrConcat(smallerRegisters): expressions.makeIntConcat(smallerRegisters); + final Expression combination = resultRegister.getType() instanceof PointerType + ? expressions.makePtrConcat(smallerRegisters): expressions.makeIntConcat(smallerRegisters); final Event computeResult = EventFactory.newLocal(load.getResultRegister(), combination); replacement.add(computeResult); return replacement; @@ -234,7 +237,9 @@ private List createTransaction(Store store, List offsets, Map visitLlvmRMW(LlvmRMW e) { String mo = e.getMo(); Register dummyReg = e.getFunction().newRegister(resultRegister.getType()); - Local localOp = newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummyReg, expressions.makeCast(expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand()),dummyReg.getType())); Load load = newRMWLoadExclusiveWithMo(resultRegister, address, ARMv8.extractLoadMoFromCMo(mo)); Store store = newRMWStoreExclusiveWithMo(address, dummyReg, true, ARMv8.extractStoreMoFromCMo(mo)); @@ -218,7 +218,7 @@ public List visitAtomicFetchOp(AtomicFetchOp e) { Register dummyReg = e.getFunction().newRegister(resultRegister.getType()); Load load = newRMWLoadExclusiveWithMo(resultRegister, address, ARMv8.extractLoadMoFromCMo(mo)); - Local localOp = newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummyReg, expressions.makeCast(expressions.makeIntBinaryForced(resultRegister, e.getOperator(), e.getOperand()), dummyReg.getType())); Store store = newRMWStoreExclusiveWithMo(address, dummyReg, true, ARMv8.extractStoreMoFromCMo(mo)); Label label = newLabel("FakeDep"); Event fakeCtrlDep = newFakeCtrlDep(resultRegister, label); @@ -379,7 +379,7 @@ public List visitLKMMCmpXchg(LKMMCmpXchg e) { Label casEnd = newLabel("CAS_end"); // The real scheme uses XOR instead of comparison, but both are semantically // equivalent and XOR harms performance substantially. - CondJump branchOnCasCmpResult = newJump(expressions.makeNEQ(dummy, e.getExpectedValue()), casEnd); + CondJump branchOnCasCmpResult = newJump(expressions.makeNEQforced(dummy, e.getExpectedValue()), casEnd); Load load = newRMWLoadExclusiveWithMo(dummy, address, ARMv8.extractLoadMoFromLKMo(mo)); Store store = newRMWStoreExclusiveWithMo(address, e.getStoreValue(), true, ARMv8.extractStoreMoFromLKMo(mo)); @@ -431,7 +431,7 @@ public List visitLKMMOpNoReturn(LKMMOpNoReturn e) { Expression address = e.getAddress(); Register dummy = e.getFunction().newRegister(e.getAccessType()); - Expression storeValue = expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand()); + Expression storeValue = expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()),dummy.getType()); Load load = newRMWLoadExclusive(dummy, address); Store store = newRMWStoreExclusive(address, storeValue, true); Label label = newLabel("FakeDep"); @@ -464,7 +464,7 @@ public List visitLKMMOpReturn(LKMMOpReturn e) { return eventSequence( load, - newLocal(dummy, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand())), + newLocal(dummy, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()),dummy.getType())), store, newLocal(resultRegister, dummy), fakeCtrlDep, @@ -485,7 +485,7 @@ public List visitLKMMFetchOp(LKMMFetchOp e) { Register dummy = e.getFunction().newRegister(resultRegister.getType()); Load load = newRMWLoadExclusiveWithMo(dummy, address, ARMv8.extractLoadMoFromLKMo(mo)); - Store store = newRMWStoreExclusiveWithMo(address, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand()), + Store store = newRMWStoreExclusiveWithMo(address, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()), dummy.getType()), true, ARMv8.extractStoreMoFromLKMo(mo)); Label label = newLabel("FakeDep"); Event fakeCtrlDep = newFakeCtrlDep(dummy, label); @@ -515,7 +515,7 @@ public List visitLKMMAddUnless(LKMMAddUnless e) { Register regValue = e.getFunction().newRegister(type); Load load = newRMWLoadExclusiveWithMo(regValue, address, ARMv8.extractLoadMoFromLKMo(mo)); - Store store = newRMWStoreExclusiveWithMo(address, expressions.makeAdd(regValue, e.getOperand()), true, ARMv8.extractStoreMoFromLKMo(mo)); + Store store = newRMWStoreExclusiveWithMo(address, expressions.makePtrAdd(regValue, e.getOperand()), true, ARMv8.extractStoreMoFromLKMo(mo)); Label label = newLabel("FakeDep"); Event fakeCtrlDep = newFakeCtrlDep(regValue, label); @@ -528,7 +528,7 @@ public List visitLKMMAddUnless(LKMMAddUnless e) { return eventSequence( load, - newLocal(dummy, expressions.makeCast(expressions.makeNEQ(regValue, unless), dummy.getType())), + newLocal(dummy, expressions.makeCast(expressions.makeNEQforced(regValue, unless), dummy.getType())), branchOnCauCmpResult, store, fakeCtrlDep, @@ -554,7 +554,7 @@ public List visitLKMMOpAndTest(LKMMOpAndTest e) { Expression testResult = expressions.makeNot(expressions.makeBooleanCast(dummy)); Load load = newRMWLoadExclusiveWithMo(dummy, address, ARMv8.extractLoadMoFromLKMo(mo)); - Local localOp = newLocal(dummy, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummy, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()), dummy.getType())); Store store = newRMWStoreExclusiveWithMo(address, dummy, true, ARMv8.extractStoreMoFromLKMo(mo)); Local testOp = newLocal(resultRegister, expressions.makeCast(testResult, resultRegister.getType())); Label label = newLabel("FakeDep"); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorPower.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorPower.java index aaff241615..95c6be0862 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorPower.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorPower.java @@ -2,6 +2,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.Type; +import com.dat3m.dartagnan.expression.integers.IntBinaryOp; import com.dat3m.dartagnan.expression.type.BooleanType; import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.program.Register; @@ -177,7 +178,7 @@ public List visitLlvmRMW(LlvmRMW e) { String mo = e.getMo(); Register dummyReg = e.getFunction().newRegister(resultRegister.getType()); - Local localOp = newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummyReg, expressions.makeCast(expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand()),dummyReg.getType())); // Power does not have mo tags, thus we use null Load load = newRMWLoadExclusive(resultRegister, address); @@ -367,7 +368,7 @@ public List visitAtomicFetchOp(AtomicFetchOp e) { String mo = e.getMo(); Register dummyReg = e.getFunction().newRegister(resultRegister.getType()); - Local localOp = newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummyReg, expressions.makeCast(expressions.makeIntBinaryForced(resultRegister,e.getOperator() ,e.getOperand()),dummyReg.getType())); Load load = newRMWLoadExclusive(resultRegister, address); Store store = Power.newRMWStoreConditional(address, dummyReg, true); @@ -658,7 +659,7 @@ public List visitLKMMCmpXchg(LKMMCmpXchg e) { Register dummy = e.getFunction().newRegister(e.getResultRegister().getType()); Label casEnd = newLabel("CAS_end"); - CondJump branchOnCasCmpResult = newJump(expressions.makeNEQ(dummy, e.getExpectedValue()), casEnd); + CondJump branchOnCasCmpResult = newJump(expressions.makeNEQforced(dummy, e.getExpectedValue()), casEnd); Load load = newRMWLoadExclusive(dummy, address); Store store = Power.newRMWStoreConditional(address, e.getStoreValue(), true); @@ -717,7 +718,7 @@ public List visitLKMMOpNoReturn(LKMMOpNoReturn e) { String mo = e.getMo(); Register dummy = e.getFunction().newRegister(e.getAccessType()); - Expression storeValue = expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand()); + Expression storeValue = expressions.makeCast(expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand()), dummy.getType()); // Power does not have mo tags, thus we use the empty string Load load = newRMWLoadExclusive(dummy, address); Store store = Power.newRMWStoreConditional(address, storeValue, true); @@ -763,7 +764,7 @@ public List visitLKMMOpReturn(LKMMOpReturn e) { return eventSequence( optionalMemoryBarrierBefore, load, - newLocal(dummy, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand())), + newLocal(dummy, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()),dummy.getType())), store, newLocal(resultRegister, dummy), fakeCtrlDep, @@ -782,7 +783,7 @@ public List visitLKMMFetchOp(LKMMFetchOp e) { Register dummy = e.getFunction().newRegister(resultRegister.getType()); Load load = newRMWLoadExclusive(dummy, address); - Store store = Power.newRMWStoreConditional(address, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand()), true); + Store store = Power.newRMWStoreConditional(address, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()), dummy.getType()), true); Label label = newLabel("FakeDep"); Event fakeCtrlDep = newFakeCtrlDep(dummy, label); @@ -817,7 +818,7 @@ public List visitLKMMAddUnless(LKMMAddUnless e) { Register regValue = e.getFunction().newRegister(type); // Power does not have mo tags, thus we use the empty string Load load = newRMWLoadExclusive(regValue, address); - Store store = Power.newRMWStoreConditional(address, expressions.makeAdd(regValue, e.getOperand()), true); + Store store = Power.newRMWStoreConditional(address, expressions.makePtrAdd(regValue, e.getOperand()), true); Label label = newLabel("FakeDep"); Event fakeCtrlDep = newFakeCtrlDep(regValue, label); @@ -834,7 +835,7 @@ public List visitLKMMAddUnless(LKMMAddUnless e) { return eventSequence( optionalMemoryBarrierBefore, load, - newLocal(dummy, expressions.makeNEQ(regValue, unless)), + newLocal(dummy, expressions.makeNEQforced(regValue, unless)), branchOnCauCmpResult, store, fakeCtrlDep, @@ -860,7 +861,7 @@ public List visitLKMMOpAndTest(LKMMOpAndTest e) { Expression testResult = expressions.makeNot(expressions.makeBooleanCast(dummy)); Load load = newRMWLoadExclusive(dummy, address); - Local localOp = newLocal(dummy, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummy, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()), dummy.getType())); Store store = Power.newRMWStoreConditional(address, dummy, true); Local testOp = newLocal(resultRegister, expressions.makeCast(testResult, resultRegister.getType())); Label label = newLabel("FakeDep"); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorRISCV.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorRISCV.java index c58379dbae..f3f05ad9ef 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorRISCV.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorRISCV.java @@ -102,7 +102,7 @@ public List visitLlvmRMW(LlvmRMW e) { String mo = e.getMo(); Register dummyReg = e.getFunction().newRegister(type); - Local localOp = newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummyReg, expressions.makeCast(expressions.makeIntBinaryForced(resultRegister, e.getOperator(), e.getOperand()),dummyReg.getType())); Load load = newRMWLoadExclusiveWithMo(resultRegister, address, Tag.RISCV.extractLoadMoFromCMo(mo)); Store store = RISCV.newRMWStoreConditional(address, dummyReg, Tag.RISCV.extractStoreMoFromCMo(mo), true); @@ -225,7 +225,7 @@ public List visitAtomicFetchOp(AtomicFetchOp e) { String mo = e.getMo(); Register dummyReg = e.getFunction().newRegister(type); - Local localOp = newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummyReg, expressions.makeCast(expressions.makeIntBinaryForced(resultRegister, e.getOperator(), e.getOperand()), dummyReg.getType())); Load load = newRMWLoadExclusiveWithMo(resultRegister, address, Tag.RISCV.extractLoadMoFromCMo(mo)); Store store = RISCV.newRMWStoreConditional(address, dummyReg, Tag.RISCV.extractStoreMoFromCMo(mo), true); @@ -395,7 +395,7 @@ public List visitLKMMCmpXchg(LKMMCmpXchg e) { Register dummy = e.getFunction().newRegister(e.getResultRegister().getType()); Register statusReg = e.getFunction().newRegister(types.getBooleanType()); Label casEnd = newLabel("CAS_end"); - CondJump branchOnCasCmpResult = newJump(expressions.makeNEQ(dummy, e.getExpectedValue()), casEnd); + CondJump branchOnCasCmpResult = newJump(expressions.makeNEQforced(dummy, e.getExpectedValue()), casEnd); Load load = newRMWLoadExclusive(dummy, address); // TODO: No mo on the load? Store store = RISCV.newRMWStoreConditional(address, e.getStoreValue(), mo.equals(Tag.Linux.MO_MB) ? Tag.RISCV.MO_REL : "", true); @@ -462,7 +462,7 @@ public List visitLKMMOpNoReturn(LKMMOpNoReturn e) { Register dummy = e.getFunction().newRegister(type); Register statusReg = e.getFunction().newRegister(types.getBooleanType()); - Expression storeValue = expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand()); + Expression storeValue = expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()), dummy.getType()); String moLoad = mo.equals(Tag.Linux.MO_MB) || mo.equals(Tag.Linux.MO_ACQUIRE) ? Tag.RISCV.MO_ACQ : ""; Load load = newRMWLoadExclusiveWithMo(dummy, address, moLoad); String moStore = mo.equals(Tag.Linux.MO_MB) || mo.equals(Tag.Linux.MO_RELEASE) ? Tag.RISCV.MO_ACQ_REL : ""; @@ -498,7 +498,7 @@ public List visitLKMMFetchOp(LKMMFetchOp e) { Register statusReg = e.getFunction().newRegister(types.getBooleanType()); Load load = newRMWLoadExclusive(dummy, address); // TODO: No mo on the load? - Store store = RISCV.newRMWStoreConditional(address, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand()), + Store store = RISCV.newRMWStoreConditional(address, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()), dummy.getType()), mo.equals(Tag.Linux.MO_MB) ? Tag.RISCV.MO_REL : "", true); ExecutionStatus status = newExecutionStatusWithDependencyTracking(statusReg, store); Label label = newLabel("FakeDep"); @@ -544,7 +544,7 @@ public List visitLKMMOpReturn(LKMMOpReturn e) { return eventSequence( optionalMemoryBarrierBefore, load, - newLocal(dummy, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand())), + newLocal(dummy, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()), dummy.getType())), store, status, newLocal(resultRegister, dummy), @@ -571,7 +571,7 @@ public List visitLKMMAddUnless(LKMMAddUnless e) { Register regValue = e.getFunction().newRegister(type); Load load = newRMWLoadExclusive(regValue, address); // TODO: No mo on the load? - Store store = RISCV.newRMWStoreConditional(address, expressions.makeAdd(regValue, e.getOperand()), mo.equals(Tag.Linux.MO_MB) ? Tag.RISCV.MO_REL : "", true); + Store store = RISCV.newRMWStoreConditional(address, expressions.makePtrAdd(regValue, e.getOperand()), mo.equals(Tag.Linux.MO_MB) ? Tag.RISCV.MO_REL : "", true); // TODO: Why does this use a different fake dep (from the load) than the other RMW events (from the store)? Label label = newLabel("FakeDep"); @@ -585,7 +585,7 @@ public List visitLKMMAddUnless(LKMMAddUnless e) { return eventSequence( load, - newLocal(dummy, expressions.makeNEQ(regValue, unless)), + newLocal(dummy, expressions.makeNEQforced(regValue, unless)), branchOnCauCmpResult, store, fakeCtrlDep, @@ -611,7 +611,7 @@ public List visitLKMMOpAndTest(LKMMOpAndTest e) { Expression testResult = expressions.makeNot(expressions.makeBooleanCast(dummy)); Load load = newRMWLoadExclusive(dummy, address); // TODO: No mo on the load? - Local localOp = newLocal(dummy, expressions.makeIntBinary(dummy, e.getOperator(), e.getOperand())); + Local localOp = newLocal(dummy, expressions.makeCast(expressions.makeIntBinaryForced(dummy, e.getOperator(), e.getOperand()), dummy.getType())); Store store = newRMWStoreExclusiveWithMo(address, dummy, true, mo.equals(Tag.Linux.MO_MB) ? Tag.RISCV.MO_REL : ""); Local testOp = newLocal(resultRegister, expressions.makeCast(testResult, resultRegister.getType())); Label label = newLabel("FakeDep"); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorTso.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorTso.java index 80b4d45f23..1739e0a1db 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorTso.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorTso.java @@ -2,6 +2,7 @@ import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.Type; +import com.dat3m.dartagnan.expression.integers.IntBinaryOp; import com.dat3m.dartagnan.expression.type.BooleanType; import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.event.Event; @@ -163,11 +164,18 @@ public List visitAtomicFetchOp(AtomicFetchOp e) { Expression address = e.getAddress(); Load load = newRMWLoad(resultRegister, address); +// return tagList(eventSequence( +// load, +// newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())), +// newRMWStore(load, address, dummyReg) +// )); return tagList(eventSequence( load, - newLocal(dummyReg, expressions.makeIntBinary(resultRegister, e.getOperator(), e.getOperand())), + newLocal(dummyReg, expressions.makeCast(expressions.makeIntBinaryForced(resultRegister,e.getOperator(),e.getOperand()),dummyReg.getType())), newRMWStore(load, address, dummyReg) )); + + } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/transformers/MemoryTransformer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/transformers/MemoryTransformer.java index f26b46853b..e042875774 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/transformers/MemoryTransformer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/transformers/MemoryTransformer.java @@ -68,9 +68,7 @@ public void setThread(Thread thread) { tid = newTid; builtIn.setThreadId(tid); registerMapping = function.getRegisters().stream().collect( - toMap(r -> r, r -> { - return thread.getOrNewRegister(r.getName(),r.getType()); - })); + toMap(r -> r, r -> thread.getOrNewRegister(r.getName(),r.getType()))); nonDetMapping = new HashMap<>(); } From eef1112fe9e2736fbb7f82700a41fa32d0fd3120 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Tue, 18 Nov 2025 22:01:31 +0100 Subject: [PATCH 25/33] added pointer simplification --- .../expression/processing/ExprSimplifier.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java index c50058a9d6..25ee846a94 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java @@ -11,6 +11,9 @@ import com.dat3m.dartagnan.expression.booleans.*; import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.ITEExpr; +import com.dat3m.dartagnan.expression.pointer.NullLiteral; +import com.dat3m.dartagnan.expression.pointer.PointerCmpOp; +import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; import com.dat3m.dartagnan.expression.utils.IntegerHelper; import com.dat3m.dartagnan.program.memory.MemoryObject; import com.google.common.base.VerifyException; @@ -186,6 +189,61 @@ public Expression visitIntCmpExpression(IntCmpExpr cmp) { return expressions.makeIntCmp(left, op, right); } + @Override + public Expression visitPtrCmpExpression(PtrCmpExpr cmp) { + final Expression rewrite = tryGeneralRewrite(cmp); + if (rewrite != null) { + return rewrite; + } + + final Expression l = cmp.getLeft().accept(this); + final Expression r = cmp.getRight().accept(this); + + // Normalize "x > y" to "y < x" (and similar). + final boolean swap = switch (cmp.getKind()) { + case GTE, GT -> true; + default -> false; + }; + final PointerCmpOp op = swap ? cmp.getKind().reverse() : cmp.getKind(); + final Expression left = swap ? r : l; + final Expression right = swap ? l : r; + + // ------- Operations on same value ------- + if (aggressive && left.equals(right)) { + return expressions.makeValue(!op.isStrict()); + } + + // ------- Operations with constants ------- + if (left instanceof NullLiteral && right instanceof NullLiteral) { + final boolean cmpResult = switch (op) { + case EQ -> true; + case NEQ -> false; + default -> + throw new VerifyException(String.format("Unexpected comparison operator '%s'.", op)); + }; + return expressions.makeValue(cmpResult); + } + + // ------- Operations on memory objects ------- + if (left instanceof MemoryObject lMem && right instanceof MemoryObject rMem) { + final boolean sameObj = lMem.equals(rMem); + + final Boolean cmpResult = switch (op) { + case EQ -> sameObj; + case NEQ -> !sameObj; + case LT-> sameObj ? false : null; + case LTE -> sameObj ? true : null; + default -> + throw new VerifyException(String.format("Unexpected comparison operator '%s'. Missing normalization?", op)); + }; + + if (cmpResult != null) { + return expressions.makeValue(cmpResult); + } + } + + return expressions.makePtrCmp(left, op, right); + } @Override public Expression visitIntSizeCastExpression(IntSizeCast expr) { From 35005fcf7dd2b9595391ba76a5a8bed00dfd882c Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Wed, 19 Nov 2025 20:50:16 +0100 Subject: [PATCH 26/33] pthread tests pass in 2.timeout --- .../expression/pointer/PointerCmpOp.java | 23 +++++++++++++++++++ .../program/processing/Intrinsics.java | 12 +++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java index a0c164fbb4..3203e105f0 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java @@ -1,6 +1,7 @@ package com.dat3m.dartagnan.expression.pointer; import com.dat3m.dartagnan.expression.ExpressionKind; +import com.dat3m.dartagnan.expression.integers.IntCmpOp; public enum PointerCmpOp implements ExpressionKind { EQ, NEQ, GTE, LTE, GT, LT; @@ -31,5 +32,27 @@ public PointerCmpOp inverted() { case LT -> GTE; }; } + public PointerCmpOp reverse() { + return switch (this) { + case EQ, NEQ -> this; + case GTE -> LTE; + case LTE -> GTE; + case GT -> LT; + case LT -> GT; + }; + } + public boolean isStrict() { + return switch (this) { + case NEQ, LT, GT -> true; + case EQ, LTE, GTE -> false; + }; + } + + public boolean isLessCategory() { + return switch (this) { + case LT, LTE-> true; + default -> false; + }; + } } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index c95ea7c3d8..6de422d6a3 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -713,14 +713,14 @@ private List inlinePthreadKeyCreate(FunctionCall call) { final Program program = call.getFunction().getProgram(); final long threadCount = program.getThreads().size(); final int pointerBytes = types.getMemorySizeInBytes(types.getPointerType()); - final Register storageAddressRegister = call.getFunction().newRegister(types.getArchType()); + final Register storageAddressRegister = call.getFunction().newRegister(types.getPointerType()); final Expression size = expressions.makeValue((threadCount + 1) * pointerBytes, types.getArchType()); final Expression destructorOffset = expressions.makeValue(threadCount * pointerBytes, types.getArchType()); //TODO call destructor at each thread's normal exit return List.of( EventFactory.newAlloc(storageAddressRegister, types.getArchType(), size, true, true), newStore(keyAddress, storageAddressRegister), - newStore(expressions.makeAdd(storageAddressRegister, destructorOffset), destructor), + newStore(expressions.makePtrAdd(storageAddressRegister, destructorOffset), destructor), assignSuccess(errorRegister) ); } @@ -741,9 +741,9 @@ private List inlinePthreadGetSpecific(FunctionCall call) { final Register result = getResultRegisterAndCheckArguments(1, call); final Expression key = call.getArguments().get(0); final int threadID = call.getThread().getId(); - final Expression offset = expressions.makeValue(threadID, (IntegerType) key.getType()); + final Expression offset = expressions.makeValue(threadID, types.getArchType()); return List.of( - EventFactory.newLoad(result, expressions.makeAdd(key, offset)) + EventFactory.newLoad(result, expressions.makePtrAdd(expressions.makePtrCast(key,types.getPointerType()), offset)) ); } @@ -753,9 +753,9 @@ private List inlinePthreadSetSpecific(FunctionCall call) { final Expression key = call.getArguments().get(0); final Expression value = call.getArguments().get(1); final int threadID = call.getThread().getId(); - final Expression offset = expressions.makeValue(threadID, (IntegerType) key.getType()); + final Expression offset = expressions.makeValue(threadID, types.getArchType()); return List.of( - newStore(expressions.makeAdd(key, offset), value), + newStore(expressions.makePtrAdd(expressions.makePtrCast(key,types.getPointerType()), offset), value), // this cast is not a good idea todo discuss it assignSuccess(errorRegister) ); } From 4139da81da8f85d8f67bf35ec3cafb0086f5cf0e Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Wed, 19 Nov 2025 21:55:08 +0100 Subject: [PATCH 27/33] fixed some asm problems and ptrCmp refactoring --- .../dartagnan/expression/ExpressionFactory.java | 11 +++++------ .../dartagnan/expression/pointer/PtrCmpExpr.java | 4 ++-- .../pointer/{PointerCmpOp.java => PtrCmpOp.java} | 7 +++---- .../expression/processing/ExprSimplifier.java | 4 ++-- .../parsers/program/visitors/VisitorAsmArm.java | 14 ++++++++++---- .../parsers/program/visitors/VisitorLlvm.java | 2 +- .../processing/NaiveLoopBoundAnnotation.java | 3 --- .../SparseConditionalConstantPropagation.java | 2 +- 8 files changed, 24 insertions(+), 23 deletions(-) rename dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/{PointerCmpOp.java => PtrCmpOp.java} (86%) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index d5d0a0af19..6c863aaa3b 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -9,7 +9,6 @@ import com.dat3m.dartagnan.expression.pointer.*; import com.dat3m.dartagnan.expression.type.*; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; -import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.memory.MemoryObject; import com.dat3m.dartagnan.program.memory.ScopedPointer; import com.dat3m.dartagnan.program.memory.ScopedPointerVariable; @@ -422,7 +421,7 @@ public Expression makeNullLiteral() { return makeNullLiteral(types.getPointerType()); } - public Expression makePtrCmp(Expression left, PointerCmpOp op, Expression right) { + public Expression makePtrCmp(Expression left, PtrCmpOp op, Expression right) { return new PtrCmpExpr(types.getBooleanType(), left, op, right); } @@ -496,7 +495,7 @@ public Expression makeEQ(Expression leftOperand, Expression rightOperand) { } else if (type instanceof IntegerType) { return makeIntCmp(leftOperand, IntCmpOp.EQ, rightOperand); } if (type instanceof PointerType) { - return makePtrCmp(leftOperand, PointerCmpOp.EQ, rightOperand); + return makePtrCmp(leftOperand, PtrCmpOp.EQ, rightOperand); }else if (type instanceof FloatType) { // TODO: Decide on a default semantics for float equality? return makeFloatCmp(leftOperand, FloatCmpOp.OEQ, rightOperand); @@ -524,7 +523,7 @@ public Expression makeNEQ(Expression leftOperand, Expression rightOperand) { } else if (type instanceof IntegerType) { return makeIntCmp(leftOperand, IntCmpOp.NEQ, rightOperand); } if (type instanceof PointerType) { - return makePtrCmp(leftOperand, PointerCmpOp.NEQ, rightOperand); + return makePtrCmp(leftOperand, PtrCmpOp.NEQ, rightOperand); }else if (type instanceof FloatType) { // TODO: Decide on a default semantics for float equality? return makeFloatCmp(leftOperand, FloatCmpOp.ONEQ, rightOperand); @@ -566,7 +565,7 @@ public Expression makeBinary(Expression x, ExpressionKind op, Expression y) { return makeFloatBinary(x, floatOp, y); } else if (op instanceof IntCmpOp cmpOp) { return makeCompare(x, cmpOp, y); - }else if (op instanceof PointerCmpOp cmpOp) { + }else if (op instanceof PtrCmpOp cmpOp) { return makeCompare(x, cmpOp, y); } throw new UnsupportedOperationException(String.format("Expression kind %s is no binary operator.", op)); @@ -579,7 +578,7 @@ public Expression makeCompare(Expression x, ExpressionKind cmpOp, Expression y) return makeFloatCmp(x, floatOp, y); } else if (cmpOp instanceof AggregateCmpOp aggrCmpOp) { return makeAggregateCmp(x, aggrCmpOp, y); - }else if (cmpOp instanceof PointerCmpOp ptrCmpOp) { + }else if (cmpOp instanceof PtrCmpOp ptrCmpOp) { return makePtrCmp(x, ptrCmpOp, y); } throw new UnsupportedOperationException(String.format("Expression kind %s is no comparison operator.", cmpOp)); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpExpr.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpExpr.java index 8b2a392847..436862b5bf 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpExpr.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpExpr.java @@ -7,9 +7,9 @@ import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.utils.ExpressionHelper; -public final class PtrCmpExpr extends BinaryExpressionBase { +public final class PtrCmpExpr extends BinaryExpressionBase { - public PtrCmpExpr(BooleanType type, Expression left, PointerCmpOp kind, Expression right) { + public PtrCmpExpr(BooleanType type, Expression left, PtrCmpOp kind, Expression right) { super(type, kind, left, right); ExpressionHelper.checkSameExpectedType(left, right, PointerType.class); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpOp.java similarity index 86% rename from dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java rename to dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpOp.java index 3203e105f0..443a071bd5 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerCmpOp.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrCmpOp.java @@ -1,9 +1,8 @@ package com.dat3m.dartagnan.expression.pointer; import com.dat3m.dartagnan.expression.ExpressionKind; -import com.dat3m.dartagnan.expression.integers.IntCmpOp; -public enum PointerCmpOp implements ExpressionKind { +public enum PtrCmpOp implements ExpressionKind { EQ, NEQ, GTE, LTE, GT, LT; @Override @@ -22,7 +21,7 @@ public String getSymbol() { case LT -> "<"; }; } - public PointerCmpOp inverted() { + public PtrCmpOp inverted() { return switch (this) { case EQ -> NEQ; case NEQ -> EQ; @@ -32,7 +31,7 @@ public PointerCmpOp inverted() { case LT -> GTE; }; } - public PointerCmpOp reverse() { + public PtrCmpOp reverse() { return switch (this) { case EQ, NEQ -> this; case GTE -> LTE; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java index 25ee846a94..8c8d3e1b8c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java @@ -12,7 +12,7 @@ import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.ITEExpr; import com.dat3m.dartagnan.expression.pointer.NullLiteral; -import com.dat3m.dartagnan.expression.pointer.PointerCmpOp; +import com.dat3m.dartagnan.expression.pointer.PtrCmpOp; import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; import com.dat3m.dartagnan.expression.utils.IntegerHelper; import com.dat3m.dartagnan.program.memory.MemoryObject; @@ -204,7 +204,7 @@ public Expression visitPtrCmpExpression(PtrCmpExpr cmp) { case GTE, GT -> true; default -> false; }; - final PointerCmpOp op = swap ? cmp.getKind().reverse() : cmp.getKind(); + final PtrCmpOp op = swap ? cmp.getKind().reverse() : cmp.getKind(); final Expression left = swap ? r : l; final Expression right = swap ? l : r; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java index bbdaf7104f..f896cce686 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorAsmArm.java @@ -10,7 +10,7 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.integers.IntCmpOp; -import com.dat3m.dartagnan.expression.pointer.PointerCmpOp; +import com.dat3m.dartagnan.expression.pointer.PtrCmpOp; import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.parsers.AsmArmBaseVisitor; @@ -277,9 +277,15 @@ public Object visitCompare(AsmArmParser.CompareContext ctx) { public Object visitCompareBranchNonZero(AsmArmParser.CompareBranchNonZeroContext ctx) { Label label = AsmUtils.getOrNewLabel(labelsDefined, ctx.Numbers().getText()); Register firstRegister = (Register) ctx.register().accept(this); - Expression zero = expressions.makeZero((IntegerType) firstRegister.getType()); - Expression expr = expressions.makeIntCmp(firstRegister, IntCmpOp.NEQ, zero); - asmInstructions.add(EventFactory.newJump(expr, label)); + if (firstRegister.getType() instanceof PointerType){ + Expression zero = expressions.makeNullLiteral( (PointerType) firstRegister.getType()); + Expression expr = expressions.makePtrCmp(firstRegister, PtrCmpOp.NEQ, zero); + asmInstructions.add(EventFactory.newJump(expr, label)); + }else{ + Expression zero = expressions.makeZero( (IntegerType) firstRegister.getType()); + Expression expr = expressions.makeIntCmp(firstRegister, IntCmpOp.NEQ, zero); + asmInstructions.add(EventFactory.newJump(expr, label)); + } return null; } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java index e80c6ca85b..8f462d86c3 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLlvm.java @@ -1072,7 +1072,7 @@ private Expression checkExpression(Type type, ParserRuleContext context) { private Expression castExpression(TypeConstContext operand, TypeContext target, boolean signed) { final Expression operandExpression = visitTypeConst(operand); final Type targetType = parseType(target); - // checkSupport(targetType instanceof IntegerType, "Non-integer type %s.", target); + checkSupport(targetType instanceof IntegerType || targetType instanceof PointerType, "Non integer or pointer type %s.", target); return expressions.makeCast(operandExpression, targetType, signed); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveLoopBoundAnnotation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveLoopBoundAnnotation.java index 3d5b9cee85..d31e015ead 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveLoopBoundAnnotation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/NaiveLoopBoundAnnotation.java @@ -4,9 +4,6 @@ import com.dat3m.dartagnan.expression.ExpressionFactory; import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.ITEExpr; -import com.dat3m.dartagnan.expression.pointer.NullLiteral; -import com.dat3m.dartagnan.expression.pointer.PointerCmpOp; -import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.Register; import com.dat3m.dartagnan.program.analysis.DominatorAnalysis; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/SparseConditionalConstantPropagation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/SparseConditionalConstantPropagation.java index 776f9af7de..bc716b4172 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/SparseConditionalConstantPropagation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/SparseConditionalConstantPropagation.java @@ -5,7 +5,6 @@ import com.dat3m.dartagnan.expression.integers.IntLiteral; import com.dat3m.dartagnan.expression.pointer.NullLiteral; import com.dat3m.dartagnan.expression.processing.ExprSimplifier; -import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.program.Function; import com.dat3m.dartagnan.program.IRHelper; import com.dat3m.dartagnan.program.Register; @@ -136,6 +135,7 @@ public void run(Function func) { final Expression expr = local.getExpr(); final Expression valueToPropagate = checkDoPropagate.test(expr) ? expr : null; propagationMap.compute(local.getResultRegister(), (k, v) -> valueToPropagate); + } else if (cur instanceof ThreadArgument arg) { // Propagate constant arguments passed across threads final Expression expr = arg.getCreator().getArguments().get(arg.getIndex()); From 02be40e85784b8e784b0aa5963123c257d1770cd Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Sat, 22 Nov 2025 18:21:41 +0100 Subject: [PATCH 28/33] improved pointer casts --- .../expression/ExpressionFactory.java | 17 +++----- .../expression/ExpressionVisitor.java | 1 + .../expression/pointer/PtrSizeCast.java | 43 +++++++++++++++++++ .../visitors/VisitorLitmusAArch64.java | 4 +- .../program/visitors/VisitorLitmusC.java | 2 +- .../program/processing/Inlining.java | 2 +- .../program/processing/Intrinsics.java | 4 +- .../dartagnan/program/processing/Tearing.java | 3 +- .../dartagnan/smt/FormulaManagerExt.java | 5 +++ 9 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrSizeCast.java diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index 6c863aaa3b..2d94856edd 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -392,9 +392,11 @@ public Expression makePtrAdd(Expression base, Expression offset) { public Expression makePtrCast(Expression base,PointerType type){ if (base.getType() instanceof PointerType ) { return base; + // todo is ptr size cast needed for mixed arm } if (base.getType() instanceof IntegerType) { - return makeIntToPtrCast(makeCast(base, types.getArchType())); + int bw = ((IntegerType) base.getType()).getBitWidth(); + return makeIntToPtrCast(makeCast(base, types.getIntegerType(bw)),types.getPointerType(bw)); } throw new UnsupportedOperationException(String.format("Cast %s into pointer unsupported.",base)); } @@ -412,7 +414,6 @@ public Expression makeIntToPtrCast(Expression operand) { return new IntToPtrCast(types.getPointerType(), operand); } - public Expression makeNullLiteral(PointerType pointerType) { return new NullLiteral(pointerType); } @@ -464,18 +465,14 @@ public Expression makeGeneralZero(Type type) { } public Expression makeCast(Expression expression, Type type, boolean signed) { - if (expression.getType().equals(type)) { - return expression; - } - - if (type instanceof BooleanType) { - return makeBooleanCast(expression); - } else if (type instanceof IntegerType integerType) { + if (expression.getType().equals(type)) {return expression;} + if (type instanceof BooleanType) {return makeBooleanCast(expression);} + else if (type instanceof IntegerType integerType) { return makeIntegerCast(expression, integerType, signed); } else if (type instanceof FloatType floatType) { return makeFloatCast(expression, floatType, signed); }else if (type instanceof PointerType) { - return makeIntToPtrCast(makeCast(expression, types.getArchType())); + return makePtrCast(expression, (PointerType) type); } throw new UnsupportedOperationException(String.format("Cast %s into %s unsupported.", expression, type)); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index f7f27e4465..54add7318d 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -65,6 +65,7 @@ public interface ExpressionVisitor { default TRet visitNullLiteral(NullLiteral lit) { return visitLeafExpression(lit); } default TRet visitPtrConcat(PtrConcat expr){ return visitExpression(expr); }; default TRet visitPtrExtract(PtrExtract expr){ return visitUnaryExpression(expr); }; + default TRet visitPtrSizeCastExpression(PtrSizeCast expr){return visitCastExpression(expr); }; // =================================== Generic =================================== default TRet visitITEExpression(ITEExpr expr) { return visitExpression(expr); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrSizeCast.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrSizeCast.java new file mode 100644 index 0000000000..ccd5a6d8b6 --- /dev/null +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrSizeCast.java @@ -0,0 +1,43 @@ +package com.dat3m.dartagnan.expression.pointer; + +import com.dat3m.dartagnan.expression.Expression; +import com.dat3m.dartagnan.expression.ExpressionVisitor; +import com.dat3m.dartagnan.expression.base.CastExpressionBase; +import com.dat3m.dartagnan.expression.type.PointerType; +import com.dat3m.dartagnan.expression.utils.ExpressionHelper; + +public final class PtrSizeCast extends CastExpressionBase { + + private final boolean preserveSign; + + public PtrSizeCast(PointerType targetType, Expression operand, boolean preserveSign) { + super(targetType, operand); + ExpressionHelper.checkExpectedType(operand, PointerType.class); + this.preserveSign = (preserveSign && isExtension()) || isNoop(); + } + + public boolean isTruncation() { + return isExtension(getTargetType(), getSourceType()); + } + + public boolean isExtension() { + return isExtension(getSourceType(), getTargetType()); + } + + public boolean isNoop() { + return getSourceType().equals(getTargetType()); + } + + public boolean preservesSign() { + return preserveSign; + } + + private static boolean isExtension(PointerType sourceType, PointerType targetType) { + return sourceType.getBitWidth() < targetType.getBitWidth(); + } + + @Override + public T accept(ExpressionVisitor visitor) { + return visitor.visitPtrSizeCastExpression(this); + } +} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java index 19ef9b902a..c35f479a5c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusAArch64.java @@ -182,7 +182,7 @@ public Object visitLoad(LoadContext ctx) { final Register register = shrinkRegister(r64, ctx.rD32, inst.halfWordSize, inst.byteSize); final Expression address = parseAddress(ctx.address()); final String mo = inst.acquire ? MO_ACQ : ""; - add(EventFactory.newLoadWithMo(register, expressions.makePtrCast(address,pointerType), mo)); + add(EventFactory.newLoadWithMo(register, expressions.makeCast(address,pointerType), mo)); addRegister64Update(r64, register); return null; } @@ -389,7 +389,7 @@ private Expression parseExpression(Expr32Context x32, Expr64Context x64) { private Expression parseAddress(AddressContext ctx) { final Register base = programBuilder.getOrErrorRegister(mainThread, ctx.register64().id); if (ctx.offset() == null) { - return expressions.makePtrCast(base,pointerType); + return expressions.makeCast(base,pointerType); } final ExpressionConversionContext conversion = ctx.offset().expressionConversion(); final Register32Context register32 = conversion == null ? null : conversion.register32(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index 05c48e3aa2..964076976e 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -588,7 +588,7 @@ public Object visitNreAssignment(LitmusCParser.NreAssignmentContext ctx){ Expression value = (Expression)ctx.re().accept(this); if(variable instanceof MemoryObject || variable instanceof Register){ - Event event = EventFactory.newStoreWithMo(variable, expressions.makePtrCast(value,pointerType), C11.NONATOMIC); + Event event = EventFactory.newStoreWithMo(variable, expressions.makeCast(value,pointerType), C11.NONATOMIC); if (isOpenCL) { event.addTags(Tag.OpenCL.DEFAULT_WEAK_SCOPE); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java index e0b7ed86ca..28e093ad34 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Inlining.java @@ -135,7 +135,7 @@ private static Event inlineBody(FunctionCall call, Snapshot callTarget, int scop for (int j = 0; j < callTarget.parameters.size(); j++) { final Register register = registerMap.get(callTarget.parameters.get(j)); if (register.getType() instanceof PointerType) { - Expression v = expressions.makePtrCast(arguments.get(j),(PointerType) register.getType()); + Expression v = expressions.makeCast(arguments.get(j),(PointerType) register.getType()); parameterAssignments.add(newLocal(register, v)); }else{ Expression v = arguments.get(j); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index 6de422d6a3..e8516294a4 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -743,7 +743,7 @@ private List inlinePthreadGetSpecific(FunctionCall call) { final int threadID = call.getThread().getId(); final Expression offset = expressions.makeValue(threadID, types.getArchType()); return List.of( - EventFactory.newLoad(result, expressions.makePtrAdd(expressions.makePtrCast(key,types.getPointerType()), offset)) + EventFactory.newLoad(result, expressions.makePtrAdd(expressions.makeCast(key,types.getPointerType()), offset)) ); } @@ -755,7 +755,7 @@ private List inlinePthreadSetSpecific(FunctionCall call) { final int threadID = call.getThread().getId(); final Expression offset = expressions.makeValue(threadID, types.getArchType()); return List.of( - newStore(expressions.makePtrAdd(expressions.makePtrCast(key,types.getPointerType()), offset), value), // this cast is not a good idea todo discuss it + newStore(expressions.makePtrAdd(expressions.makeCast(key,types.getPointerType()), offset), value), // this cast does not seem like a good idea todo discuss it assignSuccess(errorRegister) ); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java index 062a364422..2925a1e925 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Tearing.java @@ -219,8 +219,7 @@ private List createTransaction(Load load, List offsets) { private List createTransaction(Store store, List offsets, Map> map, boolean bigEndian) { final int bytes = checkBytes(store, offsets); final List replacement = new ArrayList<>(); - final PointerType addressType = checkPointerType(store.getAddress().getType(), - "Non-Pointer address in '%s'", store); + checkPointerType(store.getAddress().getType(),"Non-Pointer address in '%s'", store); checkType(store.getAccessType(), "Non-integer or Pointer mixed-size access in '%s'", store); final Function function = store.getFunction(); final Register addressRegister = toRegister(store.getAddress(), function, replacement); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/smt/FormulaManagerExt.java b/dartagnan/src/main/java/com/dat3m/dartagnan/smt/FormulaManagerExt.java index cf22aeb622..c98e9f2d7f 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/smt/FormulaManagerExt.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/smt/FormulaManagerExt.java @@ -62,6 +62,11 @@ public boolean hasSameType(Formula left, Formula right) { return false; } + public BooleanFormula isZeroBitVector(BitvectorFormula formula) { + BitvectorFormulaManager bvfm = fmgr.getBitvectorFormulaManager(); + return bvfm.equal(bvfm.makeBitvector(bvfm.getLength(formula),0), formula); + } + public BooleanFormula equal(Formula left, Formula right) { Preconditions.checkArgument(hasSameType(left, right)); From 8d5c7667263030c627c81d561c9c065fef7511c5 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Sun, 23 Nov 2025 20:21:04 +0100 Subject: [PATCH 29/33] added pointer addition simplification --- .../dartagnan/encoding/ExpressionEncoder.java | 46 +++++++++++++------ .../expression/ExpressionFactory.java | 15 +++--- .../expression/ExpressionPrinter.java | 2 +- .../expression/ExpressionVisitor.java | 2 +- .../{PointerAddExpr.java => PtrAddExpr.java} | 7 ++- .../expression/processing/ExprSimplifier.java | 22 ++++++++- .../processing/ExprTransformer.java | 2 +- .../expression/type/PointerType.java | 1 + .../expression/type/TypeFactory.java | 1 + 9 files changed, 70 insertions(+), 28 deletions(-) rename dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/{PointerAddExpr.java => PtrAddExpr.java} (82%) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index d4916eae04..b94bd96ba8 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -75,13 +75,13 @@ private BitvectorFormulaManager bitvectorFormulaManager() { @SuppressWarnings("unchecked") public TypedFormula encodeBooleanAt(Expression expression, Event at) { - Preconditions.checkArgument(expression.getType() instanceof BooleanType); + checkArgument(expression.getType() instanceof BooleanType); return (TypedFormula) encodeAt(expression, at); } @SuppressWarnings("unchecked") public TypedFormula encodeBooleanFinal(Expression expression) { - Preconditions.checkArgument(expression.getType() instanceof BooleanType); + checkArgument(expression.getType() instanceof BooleanType); return (TypedFormula) encodeFinal(expression); } @@ -93,10 +93,10 @@ public TypedFormula encodeBooleanFinal(Expression e variable = context.useIntegers ? integerFormulaManager().makeVariable(name) : bitvectorFormulaManager().makeVariable(integerType.getBitWidth(), name); - }else if (type instanceof PointerType) { + }else if (type instanceof PointerType pointerType) { variable = context.useIntegers ? integerFormulaManager().makeVariable(name) - : bitvectorFormulaManager().makeVariable(types.getArchType().getBitWidth(), name); + : bitvectorFormulaManager().makeVariable(pointerType.getBitWidth(), name); } else if (type instanceof AggregateType aggType) { final List fields = new ArrayList<>(aggType.getFields().size()); for (TypeOffset field : aggType.getFields()) { @@ -104,7 +104,7 @@ public TypedFormula encodeBooleanFinal(Expression e } variable = fmgr.getTupleFormulaManager().makeTuple(fields); } else if (type instanceof ArrayType arrType) { - Preconditions.checkArgument(arrType.hasKnownNumElements(), "Cannot encode array of unknown size."); + checkArgument(arrType.hasKnownNumElements(), "Cannot encode array of unknown size."); final List elements = new ArrayList<>(arrType.getNumElements()); for (int i = 0; i < arrType.getNumElements(); i++) { elements.add(makeVariable(name + "[" + i + "]", arrType.getElementType()).formula()); @@ -176,7 +176,7 @@ public void setEvent(Event e) { @SuppressWarnings("unchecked") public TypedFormula encodeIntegerExpr(Expression expression) { - Preconditions.checkArgument(expression.getType() instanceof IntegerType); + checkArgument(expression.getType() instanceof IntegerType); final TypedFormula typedFormula = encode(expression); assert typedFormula.getType() == expression.getType(); assert typedFormula.formula() instanceof IntegerFormula || typedFormula.formula() instanceof BitvectorFormula; @@ -194,7 +194,7 @@ public void setEvent(Event e) { @SuppressWarnings("unchecked") public TypedFormula encodeBooleanExpr(Expression expression) { - Preconditions.checkArgument(expression.getType() instanceof BooleanType); + checkArgument(expression.getType() instanceof BooleanType); final TypedFormula typedFormula = encode(expression); assert typedFormula.getType() == expression.getType(); assert typedFormula.formula() instanceof BooleanFormula; @@ -203,7 +203,7 @@ public TypedFormula encodeBooleanExpr(Expression ex @SuppressWarnings("unchecked") public TypedFormula encodeAggregateExpr(Expression expression) { - Preconditions.checkArgument(ExpressionHelper.isAggregateLike(expression)); + checkArgument(ExpressionHelper.isAggregateLike(expression)); final TypedFormula typedFormula = encode(expression); assert typedFormula.getType() == expression.getType(); assert typedFormula.formula() instanceof TupleFormula; @@ -474,7 +474,7 @@ public TypedFormula visitIntCmpExpression(IntCmpExp @Override public TypedFormula visitIntConcat(IntConcat expr) { - Preconditions.checkArgument(!expr.getOperands().isEmpty()); + checkArgument(!expr.getOperands().isEmpty()); final List> operands = expr.getOperands().stream() .map(this::encodeIntegerExpr) .toList(); @@ -519,7 +519,7 @@ public TypedFormula visitIntCmpExpression(IntCmpExp // Pointers @Override - public TypedFormula visitPointerAddExpression(PointerAddExpr expr) { + public TypedFormula visitPtrAddExpression(PtrAddExpr expr) { final TypedFormula base = encodePointerExpr(expr.getBase()); final TypedFormula offset = encodeIntegerExpr(expr.getOffset()); @@ -534,7 +534,6 @@ public TypedFormula visitIntCmpExpression(IntCmpExp return new TypedFormula<>(base.getType(), bitvectorFormulaManager().add(baseForm, offsetForm)); } - } @Override @@ -565,6 +564,14 @@ public TypedFormula visitIntCmpExpression(IntCmpExp @Override public TypedFormula visitIntToPtrCastExpression(IntToPtrCast expr) { final TypedFormula address = encodeIntegerExpr(expr.getOperand()); + if (!context.useIntegers) { + int ibw = ((IntegerType)expr.getOperand().getType()).getBitWidth(); + int pbw = expr.getType().getBitWidth(); + if (ibw(expr.getType(), fmgr.getBitvectorFormulaManager() + .extend(((BitvectorFormula) address.formula()), pbw - ibw,false)); + }} + return new TypedFormula<>(expr.getType(), address.formula()); } @@ -585,12 +592,23 @@ public TypedFormula visitPtrCmpExpression(PtrCmpExp }; return new TypedFormula<>(types.getBooleanType(), result); }else{ + final int nullLocation = expr.getLeft() instanceof NullLiteral ? 1 : ( expr.getRight() instanceof NullLiteral ? 2 : 0); // 0 means no nullpointer final BitvectorFormula left = (BitvectorFormula) encodePointerExpr(expr.getLeft()).formula(); final BitvectorFormula right = (BitvectorFormula) encodePointerExpr(expr.getRight()).formula(); final BitvectorFormulaManager bvgr = bitvectorFormulaManager(); final BooleanFormula result = switch (expr.getKind()) { - case EQ -> bvgr.equal(left, right); - case NEQ -> bmgr.not(fmgr.equal(left, right)); + case EQ -> switch (nullLocation) { + case 0 -> bvgr.equal(left, right); + case 1 -> fmgr.isZeroBitVector(right); + case 2 -> fmgr.isZeroBitVector(left); + default -> throw new IllegalStateException(); + }; + case NEQ -> bmgr.not(switch (nullLocation) { + case 0 -> bvgr.equal(left, right); + case 1 -> fmgr.isZeroBitVector(right); + case 2 -> fmgr.isZeroBitVector(left); + default -> throw new IllegalStateException(); + }); case LT -> bvgr.lessThan(left, right,false); case LTE -> bvgr.lessOrEquals(left, right,false); case GT -> bvgr.greaterThan(left, right,false); @@ -629,7 +647,7 @@ public TypedFormula visitPtrCmpExpression(PtrCmpExp @Override public TypedFormula visitPtrConcat(PtrConcat expr) { - Preconditions.checkArgument(!expr.getOperands().isEmpty()); + checkArgument(!expr.getOperands().isEmpty()); final List> operands = expr.getOperands().stream() .map(this::encodePointerExpr) .toList(); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index 2d94856edd..64c793a81d 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -386,32 +386,35 @@ public ScopedPointerVariable makeScopedPointerVariable(String id, ScopedPointerT } public Expression makePtrAdd(Expression base, Expression offset) { - return new PointerAddExpr(base, offset); + return new PtrAddExpr(base, offset); } - public Expression makePtrCast(Expression base,PointerType type){ + public Expression makePtrCast(Expression base, PointerType type, boolean tearing){ if (base.getType() instanceof PointerType ) { return base; // todo is ptr size cast needed for mixed arm } if (base.getType() instanceof IntegerType) { int bw = ((IntegerType) base.getType()).getBitWidth(); - return makeIntToPtrCast(makeCast(base, types.getIntegerType(bw)),types.getPointerType(bw)); + // this causes problems in tearing if the pointer is not of the int size + return makeIntToPtrCast(makeCast(base, types.getIntegerType(bw)),tearing?types.getPointerType(bw):types.getPointerType()); } throw new UnsupportedOperationException(String.format("Cast %s into pointer unsupported.",base)); } - public Expression makePtrToIntCast(Expression pointer) { return new PtrToIntCast(types.getArchType(), pointer); } +// public Expression makePtrToIntCast(Expression pointer, IntegerType type) { +// return new PtrToIntCast(type, pointer); +// } public Expression makeIntToPtrCast(Expression operand, PointerType pointerType) { return new IntToPtrCast(pointerType, operand); } public Expression makeIntToPtrCast(Expression operand) { - return new IntToPtrCast(types.getPointerType(), operand); + return makeIntToPtrCast(operand, types.getPointerType()); } public Expression makeNullLiteral(PointerType pointerType) { @@ -472,7 +475,7 @@ else if (type instanceof IntegerType integerType) { } else if (type instanceof FloatType floatType) { return makeFloatCast(expression, floatType, signed); }else if (type instanceof PointerType) { - return makePtrCast(expression, (PointerType) type); + return makePtrCast(expression, (PointerType) type, false); // todo fix for tearing(mixed test), maybe a teared pointer tracker. } throw new UnsupportedOperationException(String.format("Cast %s into %s unsupported.", expression, type)); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java index 9e0fcc77e8..0205506ec6 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionPrinter.java @@ -136,7 +136,7 @@ public String visitPtrToIntCastExpression(PtrToIntCast expr) { } @Override - public String visitPointerAddExpression(PointerAddExpr expr) { + public String visitPtrAddExpression(PtrAddExpr expr) { return String.format("%s + %s", visit(expr.getBase()), visit(expr.getOffset())); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index 54add7318d..9c0ada4429 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -58,7 +58,7 @@ public interface ExpressionVisitor { // =================================== Pointer =================================== default TRet visitGEPExpression(GEPExpr expr) { return visitExpression(expr); } - default TRet visitPointerAddExpression(PointerAddExpr expr) { return visitExpression(expr); } + default TRet visitPtrAddExpression(PtrAddExpr expr) { return visitExpression(expr); } default TRet visitIntToPtrCastExpression(IntToPtrCast expr) { return visitCastExpression(expr); } default TRet visitPtrToIntCastExpression(PtrToIntCast expr) { return visitCastExpression(expr); } default TRet visitPtrCmpExpression(PtrCmpExpr expr) { return visitBinaryExpression(expr); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrAddExpr.java similarity index 82% rename from dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java rename to dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrAddExpr.java index 9e5c01f19c..8c6b591102 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PointerAddExpr.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrAddExpr.java @@ -6,16 +6,15 @@ import com.dat3m.dartagnan.expression.base.ExpressionBase; import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.expression.type.PointerType; -import com.dat3m.dartagnan.expression.type.TypeFactory; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; -public class PointerAddExpr extends ExpressionBase { +public class PtrAddExpr extends ExpressionBase { private final Expression base; private final Expression offset; - public PointerAddExpr(Expression base, Expression offset) { + public PtrAddExpr(Expression base, Expression offset) { super((PointerType) base.getType()); Preconditions.checkArgument(offset.getType() instanceof IntegerType); this.base = base; @@ -37,6 +36,6 @@ public ExpressionKind getKind() { @Override public T accept(ExpressionVisitor visitor) { - return visitor.visitPointerAddExpression(this); + return visitor.visitPtrAddExpression(this); } } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java index 8c8d3e1b8c..c636d739bd 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java @@ -13,7 +13,9 @@ import com.dat3m.dartagnan.expression.misc.ITEExpr; import com.dat3m.dartagnan.expression.pointer.NullLiteral; import com.dat3m.dartagnan.expression.pointer.PtrCmpOp; +import com.dat3m.dartagnan.expression.pointer.PtrAddExpr; import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; +import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.utils.IntegerHelper; import com.dat3m.dartagnan.program.memory.MemoryObject; import com.google.common.base.VerifyException; @@ -343,7 +345,25 @@ public Expression visitIntBinaryExpression(IntBinaryExpr expr) { return expressions.makeIntBinary(left, op, right); } - // TODO: Add simplifications for IntExtract and IntConcat expressions + @Override + public Expression visitPtrAddExpression(PtrAddExpr expr) { + + final Expression base = expr.getBase().accept(this); + final Expression offset = expr.getOffset().accept(this); + + // Optimizations for "x op constant" + if (offset instanceof IntLiteral lit) { + if(lit.isZero()){return base;} + } + // fixme tearing problem + if (base instanceof NullLiteral) { + return expressions.makeIntToPtrCast(offset); + } + return expressions.makePtrAdd(base, offset); + } + + // TODO: Add simplifications for IntExtract and IntConcat expressions. + // TODO: Simplify int to ptr to int and so forth. @Override public Expression visitITEExpression(ITEExpr expr) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java index b4051dab9b..88551079e9 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java @@ -121,7 +121,7 @@ public Expression visitAggregateCmpExpression(AggregateCmpExpr expr) { @Override - public Expression visitPointerAddExpression(PointerAddExpr expr) { + public Expression visitPtrAddExpression(PtrAddExpr expr) { return expressions.makePtrAdd( expr.getBase().accept(this), expr.getOffset().accept(this) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java index 4adadd85a5..02cff5d9fa 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -5,6 +5,7 @@ public class PointerType implements Type { public int bitWidth; + public boolean teared; // todo change the encoding to recognise bit width diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java index 547957e06d..c4bfd47842 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/TypeFactory.java @@ -44,6 +44,7 @@ public BooleanType getBooleanType() { public PointerType getPointerType() { return pointerType; } + public PointerType getPointerType(int bitWidth) { // to be used in tearing checkArgument(bitWidth > 0, "Non-positive bit width %s.", bitWidth); From 0a0f0515470ed51e7226d51fee5ddceb54e25b9c Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Sun, 23 Nov 2025 22:44:24 +0100 Subject: [PATCH 30/33] restructuring of ptr <-> int encoding --- .../dartagnan/encoding/ExpressionEncoder.java | 55 ++++++++++--------- .../expression/ExpressionFactory.java | 23 +++++--- .../expression/pointer/PtrAddExpr.java | 1 - .../expression/processing/ExprSimplifier.java | 47 ++++++++++++++-- .../processing/ExprTransformer.java | 2 +- 5 files changed, 86 insertions(+), 42 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index b94bd96ba8..db2d25fbe2 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -539,38 +539,41 @@ public TypedFormula visitIntCmpExpression(IntCmpExp @Override public TypedFormula visitPtrToIntCastExpression(PtrToIntCast expr) { final TypedFormula inner = encodePointerExpr(expr.getOperand()); - final Formula enc; - if (context.useIntegers) { - if (expr.isExtension()) { - enc = inner.formula(); - }else { - final BigInteger highValue = BigInteger.TWO.pow(expr.getType().getBitWidth()); - final IntegerFormulaManager imgr = integerFormulaManager(); - enc = imgr.modulo((IntegerFormula) inner.formula(), imgr.makeNumber(highValue));} - } else { - assert inner.formula() instanceof BitvectorFormula; - final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); - final BitvectorFormula innerBv = (BitvectorFormula) inner.formula(); - final int targetBitWidth = expr.getTargetType().getBitWidth(); - final int sourceBitWidth = expr.getSourceType().getBitWidth(); - assert (sourceBitWidth == bvmgr.getLength(innerBv)); - enc = expr.isExtension() - ? bvmgr.extend(innerBv, targetBitWidth - sourceBitWidth, false) - : bvmgr.extract(innerBv, targetBitWidth - 1, 0); - } + final Formula enc = inner.formula(); + +// final Formula enc; +// if (context.useIntegers) { +// if (expr.isExtension()) { +// enc = inner.formula(); +// }else { +// final BigInteger highValue = BigInteger.TWO.pow(expr.getType().getBitWidth()); +// final IntegerFormulaManager imgr = integerFormulaManager(); +// enc = imgr.modulo((IntegerFormula) inner.formula(), imgr.makeNumber(highValue));} +// } else { +// assert inner.formula() instanceof BitvectorFormula; +// final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); +// final BitvectorFormula innerBv = (BitvectorFormula) inner.formula(); +// final int targetBitWidth = expr.getTargetType().getBitWidth(); +// final int sourceBitWidth = expr.getSourceType().getBitWidth(); +// assert (sourceBitWidth == bvmgr.getLength(innerBv)); +// enc = expr.isExtension() +// ? bvmgr.extend(innerBv, targetBitWidth - sourceBitWidth, false) +// : bvmgr.extract(innerBv, targetBitWidth - 1, 0); +// } return new TypedFormula<>(expr.getType(), enc); } @Override public TypedFormula visitIntToPtrCastExpression(IntToPtrCast expr) { final TypedFormula address = encodeIntegerExpr(expr.getOperand()); - if (!context.useIntegers) { - int ibw = ((IntegerType)expr.getOperand().getType()).getBitWidth(); - int pbw = expr.getType().getBitWidth(); - if (ibw(expr.getType(), fmgr.getBitvectorFormulaManager() - .extend(((BitvectorFormula) address.formula()), pbw - ibw,false)); - }} + // relevant for tearing +// if (!context.useIntegers) { +// int ibw = ((IntegerType)expr.getOperand().getType()).getBitWidth(); +// int pbw = expr.getType().getBitWidth(); +// if (ibw(expr.getType(), fmgr.getBitvectorFormulaManager() +// .extend(((BitvectorFormula) address.formula()), pbw - ibw,false)); +// }} return new TypedFormula<>(expr.getType(), address.formula()); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index 64c793a81d..bacdbfd04a 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -14,6 +14,7 @@ import com.dat3m.dartagnan.program.memory.ScopedPointerVariable; import com.google.common.base.Preconditions; import com.google.common.collect.Iterables; +import edu.stanford.CVC4.PrettySExprs; import java.math.BigDecimal; import java.math.BigInteger; @@ -245,7 +246,7 @@ public Expression makeIntegerCast(Expression operand, IntegerType targetType, bo } else if (sourceType instanceof FloatType) { return new FloatToIntCast(targetType, operand, signed); }else if (sourceType instanceof PointerType) { - return new PtrToIntCast(targetType, operand); + return makePtrToIntCast(operand, targetType); } throw new UnsupportedOperationException(String.format("Cannot cast %s to %s.", sourceType, targetType)); @@ -397,26 +398,32 @@ public Expression makePtrCast(Expression base, PointerType type, boolean tearing if (base.getType() instanceof IntegerType) { int bw = ((IntegerType) base.getType()).getBitWidth(); // this causes problems in tearing if the pointer is not of the int size - return makeIntToPtrCast(makeCast(base, types.getIntegerType(bw)),tearing?types.getPointerType(bw):types.getPointerType()); + return makeIntToPtrCast(makeCast(base, types.getIntegerType(bw)),tearing ? types.getPointerType(bw):types.getPointerType()); } throw new UnsupportedOperationException(String.format("Cast %s into pointer unsupported.",base)); } + + + public Expression makePtrToIntCast(Expression pointer) { - return new PtrToIntCast(types.getArchType(), pointer); + return makePtrToIntCast(pointer, types.getArchType()); + } + public Expression makePtrToIntCast(Expression pointer, IntegerType type) { + return makeIntegerCast( new PtrToIntCast(types.getIntegerType(((PointerType)pointer.getType()).getBitWidth()), pointer), type,false); } -// public Expression makePtrToIntCast(Expression pointer, IntegerType type) { -// return new PtrToIntCast(type, pointer); -// } public Expression makeIntToPtrCast(Expression operand, PointerType pointerType) { - return new IntToPtrCast(pointerType, operand); + return new IntToPtrCast(pointerType, makeIntegerCast(operand,types.getArchType(),false)); } - public Expression makeIntToPtrCast(Expression operand) { return makeIntToPtrCast(operand, types.getPointerType()); } + + + + public Expression makeNullLiteral(PointerType pointerType) { return new NullLiteral(pointerType); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrAddExpr.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrAddExpr.java index 8c6b591102..cef4509598 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrAddExpr.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrAddExpr.java @@ -1,5 +1,4 @@ package com.dat3m.dartagnan.expression.pointer; - import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.ExpressionKind; import com.dat3m.dartagnan.expression.ExpressionVisitor; diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java index c636d739bd..844700a249 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java @@ -11,10 +11,7 @@ import com.dat3m.dartagnan.expression.booleans.*; import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.ITEExpr; -import com.dat3m.dartagnan.expression.pointer.NullLiteral; -import com.dat3m.dartagnan.expression.pointer.PtrCmpOp; -import com.dat3m.dartagnan.expression.pointer.PtrAddExpr; -import com.dat3m.dartagnan.expression.pointer.PtrCmpExpr; +import com.dat3m.dartagnan.expression.pointer.*; import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.utils.IntegerHelper; import com.dat3m.dartagnan.program.memory.MemoryObject; @@ -363,7 +360,46 @@ public Expression visitPtrAddExpression(PtrAddExpr expr) { } // TODO: Add simplifications for IntExtract and IntConcat expressions. - // TODO: Simplify int to ptr to int and so forth. + + // Simplifies (int(i) -> ptr(i)) -> int(i). + @Override + public Expression visitPtrToIntCastExpression(PtrToIntCast expr) { + final Expression sub = expr.getOperand().accept(this); + if (sub instanceof IntToPtrCast subT){ + final int originalBitWidth = subT.getSourceType().getBitWidth(); + final int inBetweenBitWidth = subT.getTargetType().getBitWidth(); + final int finalBitWidth = expr.getType().getBitWidth(); + + if (originalBitWidth == inBetweenBitWidth && finalBitWidth == originalBitWidth ) { + return subT.getOperand(); + } + } +// if (sub instanceof NullLiteral) { +// return expressions.makeZero(expr.getType()); +// } // the problem here is that + return expressions.makePtrToIntCast(expr.getOperand(), expr.getType()); + } + + // Simplifies (ptr(i) -> int(i)) -> ptr(i). + @Override + public Expression visitIntToPtrCastExpression(IntToPtrCast expr) { + final Expression sub = expr.getOperand().accept(this); + if (sub instanceof PtrToIntCast subT){ + final int originalBitWidth = subT.getSourceType().getBitWidth(); + final int inBetweenBitWidth = subT.getTargetType().getBitWidth(); + final int finalBitWidth = expr.getType().getBitWidth(); + + if (originalBitWidth == inBetweenBitWidth && finalBitWidth == originalBitWidth ) { + return subT.getOperand(); + } + } + if (sub instanceof IntLiteral && ((IntLiteral) sub).isZero()) { + return expressions.makeNullLiteral(expr.getType()); + } + return expressions.makeIntToPtrCast(expr.getOperand(), expr.getType()); + } + + @Override public Expression visitITEExpression(ITEExpr expr) { @@ -455,7 +491,6 @@ public Expression visitAggregateCmpExpression(AggregateCmpExpr expr) { } // =================================== Helper methods =================================== - // An expression is potentially eliminable if it either carries no dependencies // or we are in aggressive mode. private boolean isPotentiallyEliminable(Expression expr) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java index 88551079e9..8b63c1f2d2 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java @@ -139,7 +139,7 @@ public Expression visitPtrCmpExpression(PtrCmpExpr expr) { @Override public Expression visitPtrToIntCastExpression(PtrToIntCast expr) { - return expressions.makePtrToIntCast(expr.getOperand().accept(this)); + return expressions.makeIntegerCast(expressions.makePtrToIntCast(expr.getOperand().accept(this)),expr.getType(),false); } @Override From 3f1d54a236545523629b2d50b06260f0d583d920 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Wed, 26 Nov 2025 20:44:22 +0100 Subject: [PATCH 31/33] - improved type recognition in c11 litmus - deleted ptr size cast - function-pointer ++ --- .../expression/ExpressionFactory.java | 9 ++-- .../expression/ExpressionVisitor.java | 2 +- .../expression/pointer/PtrSizeCast.java | 43 ------------------- .../expression/processing/ExprSimplifier.java | 12 +++--- .../processing/ExprTransformer.java | 2 +- .../program/visitors/VisitorLitmusC.java | 27 +++++++++--- .../com/dat3m/dartagnan/program/Function.java | 2 +- .../program/processing/ProcessingManager.java | 4 +- .../processing/compilation/VisitorC11.java | 2 +- 9 files changed, 39 insertions(+), 64 deletions(-) delete mode 100644 dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrSizeCast.java diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index bacdbfd04a..ec355ccd96 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -393,12 +393,16 @@ public Expression makePtrAdd(Expression base, Expression offset) { public Expression makePtrCast(Expression base, PointerType type, boolean tearing){ if (base.getType() instanceof PointerType ) { return base; - // todo is ptr size cast needed for mixed arm + // todo is ptr size cast needed for mixed arm (tearing) } if (base.getType() instanceof IntegerType) { int bw = ((IntegerType) base.getType()).getBitWidth(); // this causes problems in tearing if the pointer is not of the int size return makeIntToPtrCast(makeCast(base, types.getIntegerType(bw)),tearing ? types.getPointerType(bw):types.getPointerType()); + } + if (base.getType() instanceof BooleanType) { + + } throw new UnsupportedOperationException(String.format("Cast %s into pointer unsupported.",base)); } @@ -406,7 +410,7 @@ public Expression makePtrCast(Expression base, PointerType type, boolean tearing - public Expression makePtrToIntCast(Expression pointer) { + public Expression makePtrToIntCast(Expression pointer) { // todo fix return makePtrToIntCast(pointer, types.getArchType()); } public Expression makePtrToIntCast(Expression pointer, IntegerType type) { @@ -551,7 +555,6 @@ public Expression makeNEQforced(Expression leftOperand, Expression rightOperand) return makeNEQ(leftOperand, rightOperand); } - public Expression makeUnary(ExpressionKind op, Expression expr) { if (op instanceof BoolUnaryOp boolOp) { return makeBoolUnary(boolOp, expr); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index 9c0ada4429..091acfc341 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -65,7 +65,7 @@ public interface ExpressionVisitor { default TRet visitNullLiteral(NullLiteral lit) { return visitLeafExpression(lit); } default TRet visitPtrConcat(PtrConcat expr){ return visitExpression(expr); }; default TRet visitPtrExtract(PtrExtract expr){ return visitUnaryExpression(expr); }; - default TRet visitPtrSizeCastExpression(PtrSizeCast expr){return visitCastExpression(expr); }; + //default TRet visitPtrSizeCastExpression(PtrSizeCast expr){return visitCastExpression(expr); }; // =================================== Generic =================================== default TRet visitITEExpression(ITEExpr expr) { return visitExpression(expr); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrSizeCast.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrSizeCast.java deleted file mode 100644 index ccd5a6d8b6..0000000000 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/pointer/PtrSizeCast.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.dat3m.dartagnan.expression.pointer; - -import com.dat3m.dartagnan.expression.Expression; -import com.dat3m.dartagnan.expression.ExpressionVisitor; -import com.dat3m.dartagnan.expression.base.CastExpressionBase; -import com.dat3m.dartagnan.expression.type.PointerType; -import com.dat3m.dartagnan.expression.utils.ExpressionHelper; - -public final class PtrSizeCast extends CastExpressionBase { - - private final boolean preserveSign; - - public PtrSizeCast(PointerType targetType, Expression operand, boolean preserveSign) { - super(targetType, operand); - ExpressionHelper.checkExpectedType(operand, PointerType.class); - this.preserveSign = (preserveSign && isExtension()) || isNoop(); - } - - public boolean isTruncation() { - return isExtension(getTargetType(), getSourceType()); - } - - public boolean isExtension() { - return isExtension(getSourceType(), getTargetType()); - } - - public boolean isNoop() { - return getSourceType().equals(getTargetType()); - } - - public boolean preservesSign() { - return preserveSign; - } - - private static boolean isExtension(PointerType sourceType, PointerType targetType) { - return sourceType.getBitWidth() < targetType.getBitWidth(); - } - - @Override - public T accept(ExpressionVisitor visitor) { - return visitor.visitPtrSizeCastExpression(this); - } -} diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java index 844700a249..642a6ff24c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java @@ -352,9 +352,9 @@ public Expression visitPtrAddExpression(PtrAddExpr expr) { if (offset instanceof IntLiteral lit) { if(lit.isZero()){return base;} } - // fixme tearing problem + // can cause a tearing problem if (base instanceof NullLiteral) { - return expressions.makeIntToPtrCast(offset); + return expressions.makeIntToPtrCast(offset,expr.getType()); } return expressions.makePtrAdd(base, offset); } @@ -374,9 +374,9 @@ public Expression visitPtrToIntCastExpression(PtrToIntCast expr) { return subT.getOperand(); } } -// if (sub instanceof NullLiteral) { -// return expressions.makeZero(expr.getType()); -// } // the problem here is that + if (sub instanceof NullLiteral) { + return expressions.makeZero(expr.getType()); + } // the problem here is that return expressions.makePtrToIntCast(expr.getOperand(), expr.getType()); } @@ -399,8 +399,6 @@ public Expression visitIntToPtrCastExpression(IntToPtrCast expr) { return expressions.makeIntToPtrCast(expr.getOperand(), expr.getType()); } - - @Override public Expression visitITEExpression(ITEExpr expr) { final Expression cond = expr.getCondition().accept(this); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java index 8b63c1f2d2..cf780c6304 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java @@ -139,7 +139,7 @@ public Expression visitPtrCmpExpression(PtrCmpExpr expr) { @Override public Expression visitPtrToIntCastExpression(PtrToIntCast expr) { - return expressions.makeIntegerCast(expressions.makePtrToIntCast(expr.getOperand().accept(this)),expr.getType(),false); + return expressions.makeIntegerCast(expressions.makePtrToIntCast(expr.getOperand().accept(this)), expr.getType(),false); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index 964076976e..1a8b541eab 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -4,6 +4,7 @@ import com.dat3m.dartagnan.exception.ParsingException; import com.dat3m.dartagnan.expression.Expression; import com.dat3m.dartagnan.expression.ExpressionFactory; +import com.dat3m.dartagnan.expression.Type; import com.dat3m.dartagnan.expression.integers.IntLiteral; import com.dat3m.dartagnan.expression.type.IntegerType; import com.dat3m.dartagnan.expression.type.PointerType; @@ -348,7 +349,7 @@ public Expression visitReXchg(LitmusCParser.ReXchgContext ctx){ @Override public Expression visitReC11SCmpXchgExplicit(LitmusCParser.ReC11SCmpXchgExplicitContext ctx) { - Register register = getReturnRegister(true); + Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); Expression value = (Expression)ctx.value.accept(this); Expression address = getAddress(ctx.address); Expression expectedAdd = getAddress(ctx.expectedAdd); @@ -361,7 +362,7 @@ public Expression visitReC11SCmpXchgExplicit(LitmusCParser.ReC11SCmpXchgExplicit @Override public Expression visitReC11SCmpXchg(LitmusCParser.ReC11SCmpXchgContext ctx) { - Register register = getReturnRegister(true); + Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); Expression value = (Expression)ctx.value.accept(this); Expression address = getAddress(ctx.address); Expression expectedAdd = getAddress(ctx.expectedAdd); @@ -374,7 +375,7 @@ public Expression visitReC11SCmpXchg(LitmusCParser.ReC11SCmpXchgContext ctx) { @Override public Expression visitReC11WCmpXchgExplicit(LitmusCParser.ReC11WCmpXchgExplicitContext ctx) { - Register register = getReturnRegister(true); + Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); Expression value = (Expression)ctx.value.accept(this); Expression address = getAddress(ctx.address); Expression expectedAdd = getAddress(ctx.expectedAdd); @@ -387,7 +388,7 @@ public Expression visitReC11WCmpXchgExplicit(LitmusCParser.ReC11WCmpXchgExplicit @Override public Expression visitReC11WCmpXchg(LitmusCParser.ReC11WCmpXchgContext ctx) { - Register register = getReturnRegister(true); + Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); Expression value = (Expression)ctx.value.accept(this); Expression address = getAddress(ctx.address); Expression expectedAdd = getAddress(ctx.expectedAdd); @@ -400,7 +401,7 @@ public Expression visitReC11WCmpXchg(LitmusCParser.ReC11WCmpXchgContext ctx) { @Override public Expression visitReCmpXchg(LitmusCParser.ReCmpXchgContext ctx){ - Register register = getReturnRegister(true); + Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); Expression cmp = (Expression)ctx.cmp.accept(this); Expression value = (Expression)ctx.value.accept(this); Event event = EventFactory.Linux.newRMWCompareExchange(getAddress(ctx.address), register, cmp, value, ctx.mo); @@ -600,8 +601,13 @@ public Object visitNreAssignment(LitmusCParser.NreAssignmentContext ctx){ @Override public Object visitNreRegDeclaration(LitmusCParser.NreRegDeclarationContext ctx){ Register register = programBuilder.getRegister(scope, ctx.varName().getText()); + if(register == null){ - register = programBuilder.getOrNewRegister(scope, ctx.varName().getText(), pointerType); //care + System.out.println(ctx.typeSpecifier().basicTypeSpecifier().getText()); + if (ctx.typeSpecifier().basicTypeSpecifier().getText().endsWith("*")){ + register = programBuilder.getOrNewRegister(scope, ctx.varName().getText(), pointerType); + }else{ + register = programBuilder.getOrNewRegister(scope, ctx.varName().getText(), archType);} //care if(ctx.re() != null){ returnRegister = register; ctx.re().accept(this); @@ -700,6 +706,7 @@ private Expression getAddress(LitmusCParser.ReContext ctx){ throw new ParsingException("Invalid syntax near " + ctx.getText()); } + private Expression returnExpressionOrOne(LitmusCParser.ReContext ctx) { return ctx != null ? (Expression) ctx.accept(this) : expressions.makeOne(archType); } @@ -712,6 +719,14 @@ private Register getReturnRegister(boolean createOnNull){ returnRegister = null; return register; } + private Register getReturnRegister(boolean createOnNull, Type t){ + Register register = returnRegister; + if(register == null && createOnNull){ + return programBuilder.getOrNewRegister(scope, null, t); + } + returnRegister = null; + return register; + } private Expression assignToReturnRegister(Register register, Expression value) { if (register != null) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/Function.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/Function.java index bf51489cf2..4935f0713d 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/Function.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/Function.java @@ -62,7 +62,7 @@ public Function(String name, FunctionType type, List parameterNames, int @Override public Type getType() { - return TypeFactory.getInstance().getArchType(); + return TypeFactory.getInstance().getPointerType(); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java index 3e0816254a..55e0808a05 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java @@ -147,10 +147,12 @@ private ProcessingManager(Configuration config) throws InvalidConfigurationExcep MemoryAllocation.fromConfig(config), detectMixedSizeAccesses ? Tearing.fromConfig(config) : null, detectMixedSizeAccesses ? simplifyBoundedProgram : null, + //DebugPrint.withHeader("Before non termination", Printer.Mode.ALL, config), NonterminationDetection.fromConfig(config), + //DebugPrint.withHeader("After non termination", Printer.Mode.ALL, config), // --- Statistics + verification --- - IdReassignment.newInstance(), // Normalize used Ids (remove any gaps) printAfterProcessing ? DebugPrint.withHeader("After processing", Printer.Mode.THREADS, config) : null, + IdReassignment.newInstance(), // Normalize used Ids (remove any gaps) ProgramProcessor.fromFunctionProcessor( CoreCodeVerification.fromConfig(config), Target.THREADS, false diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorC11.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorC11.java index 88de5e37e7..13bcc9a1f7 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorC11.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/compilation/VisitorC11.java @@ -44,7 +44,7 @@ public List visitAtomicCmpXchg(AtomicCmpXchg e) { Expression address = e.getAddress(); String mo = e.getMo(); Expression expectedAddr = e.getAddressOfExpected(); - Type type = resultRegister.getType(); + Type type = resultRegister.getType(); // todo how can this be a pointer ? Register booleanResultRegister = type instanceof BooleanType ? resultRegister : e.getFunction().newRegister(types.getBooleanType()); Local castResult = type instanceof BooleanType ? null : From 2c5a8f0e5e5beb1e944f76a05204df8e73d03eb9 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Wed, 26 Nov 2025 21:34:53 +0100 Subject: [PATCH 32/33] fixed litmus type mismatch --- .../dartagnan/expression/ExpressionFactory.java | 2 +- .../parsers/program/visitors/VisitorLitmusC.java | 16 +++++++--------- .../program/processing/ProcessingManager.java | 3 --- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index ec355ccd96..c1e2deea84 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -507,7 +507,7 @@ public Expression makeEQ(Expression leftOperand, Expression rightOperand) { return makeIntCmp(leftOperand, IntCmpOp.EQ, rightOperand); } if (type instanceof PointerType) { return makePtrCmp(leftOperand, PtrCmpOp.EQ, rightOperand); - }else if (type instanceof FloatType) { + } else if (type instanceof FloatType) { // TODO: Decide on a default semantics for float equality? return makeFloatCmp(leftOperand, FloatCmpOp.OEQ, rightOperand); } else if (ExpressionHelper.isAggregateLike(type)) { diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index 1a8b541eab..94865645e9 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -349,7 +349,7 @@ public Expression visitReXchg(LitmusCParser.ReXchgContext ctx){ @Override public Expression visitReC11SCmpXchgExplicit(LitmusCParser.ReC11SCmpXchgExplicitContext ctx) { - Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); + Register register = getReturnRegister(TypeFactory.getInstance().getArchType()); Expression value = (Expression)ctx.value.accept(this); Expression address = getAddress(ctx.address); Expression expectedAdd = getAddress(ctx.expectedAdd); @@ -362,7 +362,7 @@ public Expression visitReC11SCmpXchgExplicit(LitmusCParser.ReC11SCmpXchgExplicit @Override public Expression visitReC11SCmpXchg(LitmusCParser.ReC11SCmpXchgContext ctx) { - Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); + Register register = getReturnRegister(TypeFactory.getInstance().getArchType()); Expression value = (Expression)ctx.value.accept(this); Expression address = getAddress(ctx.address); Expression expectedAdd = getAddress(ctx.expectedAdd); @@ -375,7 +375,7 @@ public Expression visitReC11SCmpXchg(LitmusCParser.ReC11SCmpXchgContext ctx) { @Override public Expression visitReC11WCmpXchgExplicit(LitmusCParser.ReC11WCmpXchgExplicitContext ctx) { - Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); + Register register = getReturnRegister(TypeFactory.getInstance().getArchType()); Expression value = (Expression)ctx.value.accept(this); Expression address = getAddress(ctx.address); Expression expectedAdd = getAddress(ctx.expectedAdd); @@ -388,7 +388,7 @@ public Expression visitReC11WCmpXchgExplicit(LitmusCParser.ReC11WCmpXchgExplicit @Override public Expression visitReC11WCmpXchg(LitmusCParser.ReC11WCmpXchgContext ctx) { - Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); + Register register = getReturnRegister(TypeFactory.getInstance().getArchType()); Expression value = (Expression)ctx.value.accept(this); Expression address = getAddress(ctx.address); Expression expectedAdd = getAddress(ctx.expectedAdd); @@ -401,7 +401,7 @@ public Expression visitReC11WCmpXchg(LitmusCParser.ReC11WCmpXchgContext ctx) { @Override public Expression visitReCmpXchg(LitmusCParser.ReCmpXchgContext ctx){ - Register register = getReturnRegister(true,TypeFactory.getInstance().getBooleanType()); + Register register = getReturnRegister(TypeFactory.getInstance().getArchType()); Expression cmp = (Expression)ctx.cmp.accept(this); Expression value = (Expression)ctx.value.accept(this); Event event = EventFactory.Linux.newRMWCompareExchange(getAddress(ctx.address), register, cmp, value, ctx.mo); @@ -719,11 +719,9 @@ private Register getReturnRegister(boolean createOnNull){ returnRegister = null; return register; } - private Register getReturnRegister(boolean createOnNull, Type t){ + private Register getReturnRegister(Type t){ Register register = returnRegister; - if(register == null && createOnNull){ - return programBuilder.getOrNewRegister(scope, null, t); - } + if(register == null){return programBuilder.getOrNewRegister(scope, null, t);} returnRegister = null; return register; } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java index 55e0808a05..a0aa87a6ee 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ProcessingManager.java @@ -132,7 +132,6 @@ private ProcessingManager(Configuration config) throws InvalidConfigurationExcep ), ThreadCreation.fromConfig(config), - //DebugPrint.withHeader("--------Debug--------", Printer.Mode.ALL, config), ResolveNonDetChoices.newInstance(), reduceSymmetry ? SymmetryReduction.fromConfig(config) : null, intrinsics.lateInliningPass(), @@ -147,9 +146,7 @@ private ProcessingManager(Configuration config) throws InvalidConfigurationExcep MemoryAllocation.fromConfig(config), detectMixedSizeAccesses ? Tearing.fromConfig(config) : null, detectMixedSizeAccesses ? simplifyBoundedProgram : null, - //DebugPrint.withHeader("Before non termination", Printer.Mode.ALL, config), NonterminationDetection.fromConfig(config), - //DebugPrint.withHeader("After non termination", Printer.Mode.ALL, config), // --- Statistics + verification --- printAfterProcessing ? DebugPrint.withHeader("After processing", Printer.Mode.THREADS, config) : null, IdReassignment.newInstance(), // Normalize used Ids (remove any gaps) From 05a2d183832c10c7f067b45b2901f23dcf60fc57 Mon Sep 17 00:00:00 2001 From: Mahdhaoui Date: Fri, 28 Nov 2025 20:07:04 +0100 Subject: [PATCH 33/33] refactoring --- .../dartagnan/encoding/ExpressionEncoder.java | 56 +++++++++---------- .../dartagnan/encoding/ProgramEncoder.java | 4 +- .../expression/ExpressionFactory.java | 46 +++++++-------- .../expression/ExpressionVisitor.java | 1 - .../expression/processing/ExprSimplifier.java | 5 +- .../processing/ExprTransformer.java | 2 +- .../expression/type/PointerType.java | 4 +- .../program/visitors/VisitorLitmusC.java | 3 +- .../program/processing/Intrinsics.java | 48 ++++++++-------- .../program/processing/ThreadCreation.java | 3 +- .../others/miscellaneous/AnalysisTest.java | 2 +- 11 files changed, 84 insertions(+), 90 deletions(-) diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java index db2d25fbe2..d0fd619d03 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ExpressionEncoder.java @@ -539,41 +539,39 @@ public TypedFormula visitIntCmpExpression(IntCmpExp @Override public TypedFormula visitPtrToIntCastExpression(PtrToIntCast expr) { final TypedFormula inner = encodePointerExpr(expr.getOperand()); - final Formula enc = inner.formula(); - -// final Formula enc; -// if (context.useIntegers) { -// if (expr.isExtension()) { -// enc = inner.formula(); -// }else { -// final BigInteger highValue = BigInteger.TWO.pow(expr.getType().getBitWidth()); -// final IntegerFormulaManager imgr = integerFormulaManager(); -// enc = imgr.modulo((IntegerFormula) inner.formula(), imgr.makeNumber(highValue));} -// } else { -// assert inner.formula() instanceof BitvectorFormula; -// final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); -// final BitvectorFormula innerBv = (BitvectorFormula) inner.formula(); -// final int targetBitWidth = expr.getTargetType().getBitWidth(); -// final int sourceBitWidth = expr.getSourceType().getBitWidth(); -// assert (sourceBitWidth == bvmgr.getLength(innerBv)); -// enc = expr.isExtension() -// ? bvmgr.extend(innerBv, targetBitWidth - sourceBitWidth, false) -// : bvmgr.extract(innerBv, targetBitWidth - 1, 0); -// } + final Formula enc; + if (context.useIntegers) { + if (expr.isExtension()) { + enc = inner.formula(); + }else { + final BigInteger highValue = BigInteger.TWO.pow(expr.getType().getBitWidth()); + final IntegerFormulaManager imgr = integerFormulaManager(); + enc = imgr.modulo((IntegerFormula) inner.formula(), imgr.makeNumber(highValue));} + } else { + assert inner.formula() instanceof BitvectorFormula; + final BitvectorFormulaManager bvmgr = bitvectorFormulaManager(); + final BitvectorFormula innerBv = (BitvectorFormula) inner.formula(); + final int targetBitWidth = expr.getTargetType().getBitWidth(); + final int sourceBitWidth = expr.getSourceType().getBitWidth(); + assert (sourceBitWidth == bvmgr.getLength(innerBv)); + enc = expr.isExtension() + ? bvmgr.extend(innerBv, targetBitWidth - sourceBitWidth, false) + : bvmgr.extract(innerBv, targetBitWidth - 1, 0); + } return new TypedFormula<>(expr.getType(), enc); } @Override public TypedFormula visitIntToPtrCastExpression(IntToPtrCast expr) { final TypedFormula address = encodeIntegerExpr(expr.getOperand()); - // relevant for tearing -// if (!context.useIntegers) { -// int ibw = ((IntegerType)expr.getOperand().getType()).getBitWidth(); -// int pbw = expr.getType().getBitWidth(); -// if (ibw(expr.getType(), fmgr.getBitvectorFormulaManager() -// .extend(((BitvectorFormula) address.formula()), pbw - ibw,false)); -// }} + // todo add support for lossy cast like int128 to ptr(is it even possible?) + if (!context.useIntegers) { + int ibw = ((IntegerType)expr.getOperand().getType()).getBitWidth(); + int pbw = expr.getType().getBitWidth(); + if (ibw(expr.getType(), fmgr.getBitvectorFormulaManager() + .extend(((BitvectorFormula) address.formula()), pbw - ibw,false)); + }} return new TypedFormula<>(expr.getType(), address.formula()); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ProgramEncoder.java b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ProgramEncoder.java index 42c5c44157..ecf4d1e6f1 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ProgramEncoder.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/encoding/ProgramEncoder.java @@ -426,7 +426,7 @@ private BooleanFormula encodeMemoryLayout(Memory memory) { final List memoryObjects = ImmutableList.copyOf(memory.getObjects()); for (int i = 0; i < memoryObjects.size(); i++) { final MemoryObject cur = memoryObjects.get(i); - final Expression addrVar = exprs.makePtrToIntCast(context.address(cur)); + final Expression addrVar = exprs.makePtrToIntCast(context.address(cur), archType); final Expression sizeVar = context.size(cur); final Expression size; @@ -464,7 +464,7 @@ private BooleanFormula encodeMemoryLayout(Memory memory) { enc.add(equate.apply(addrVar, alignment)); } else { final Expression nextAvailableAddr = exprs.makeAdd( - exprs.makePtrToIntCast(context.address(prev)), + exprs.makePtrToIntCast(context.address(prev),archType), context.size(prev) ); final Expression nextAlignedAddr = exprs.makeAdd(nextAvailableAddr, diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java index c1e2deea84..001b45ed7b 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionFactory.java @@ -14,7 +14,6 @@ import com.dat3m.dartagnan.program.memory.ScopedPointerVariable; import com.google.common.base.Preconditions; import com.google.common.collect.Iterables; -import edu.stanford.CVC4.PrettySExprs; import java.math.BigDecimal; import java.math.BigInteger; @@ -80,7 +79,7 @@ public Expression makeBooleanCast(Expression operand) { } else if (sourceType instanceof IntegerType intType) { return makeNEQ(operand, makeZero(intType)); }else if (sourceType instanceof PointerType) { - return makeBooleanCast(makePtrToIntCast(operand)); + return makeBooleanCast(makePtrToIntCast(operand, types.getArchType())); } throw new UnsupportedOperationException(String.format("Cannot cast %s to %s.", sourceType, booleanType)); } @@ -214,10 +213,10 @@ public Expression makeIntCmp(Expression leftOperand, IntCmpOp operator, Expressi public Expression makeIntCmpForced(Expression leftOperand, IntCmpOp operator, Expression rightOperand) { if (leftOperand.getType() instanceof PointerType){ - return makeIntCmpForced(makePtrToIntCast(leftOperand), operator, rightOperand); + return makeIntCmpForced(makePtrToIntCast(leftOperand, types.getArchType()), operator, rightOperand); } if (rightOperand.getType() instanceof PointerType){ - return makeIntCmpForced(leftOperand, operator, makePtrToIntCast(rightOperand)); + return makeIntCmpForced(leftOperand, operator, makePtrToIntCast(rightOperand, types.getArchType())); } return new IntCmpExpr(types.getBooleanType(), leftOperand, operator, rightOperand); } @@ -228,10 +227,10 @@ public Expression makeIntBinary(Expression leftOperand, IntBinaryOp operator, Ex public Expression makeIntBinaryForced(Expression leftOperand, IntBinaryOp operator, Expression rightOperand) { if (leftOperand.getType() instanceof PointerType){ - return makeIntBinaryForced(makePtrToIntCast(leftOperand), operator, rightOperand); + return makeIntBinaryForced(makePtrToIntCast(leftOperand, types.getArchType()), operator, rightOperand); } if (rightOperand.getType() instanceof PointerType){ - return makeIntBinaryForced(leftOperand, operator, makePtrToIntCast(rightOperand)); + return makeIntBinaryForced(leftOperand, operator, makePtrToIntCast(rightOperand, types.getArchType())); } return new IntBinaryExpr(leftOperand, operator, rightOperand); } @@ -390,35 +389,30 @@ public Expression makePtrAdd(Expression base, Expression offset) { return new PtrAddExpr(base, offset); } - public Expression makePtrCast(Expression base, PointerType type, boolean tearing){ + public Expression makePtrCast(Expression base, PointerType type){ if (base.getType() instanceof PointerType ) { return base; // todo is ptr size cast needed for mixed arm (tearing) } if (base.getType() instanceof IntegerType) { - int bw = ((IntegerType) base.getType()).getBitWidth(); + //int bw = ((IntegerType) base.getType()).getBitWidth(); // this causes problems in tearing if the pointer is not of the int size - return makeIntToPtrCast(makeCast(base, types.getIntegerType(bw)),tearing ? types.getPointerType(bw):types.getPointerType()); - } - if (base.getType() instanceof BooleanType) { - - + // return makeIntToPtrCast(makeCast(base, types.getIntegerType(bw)),tearing ? types.getPointerType(bw):types.getPointerType()); + return makeIntToPtrCast(base); } throw new UnsupportedOperationException(String.format("Cast %s into pointer unsupported.",base)); } - - public Expression makePtrToIntCast(Expression pointer) { // todo fix - return makePtrToIntCast(pointer, types.getArchType()); - } public Expression makePtrToIntCast(Expression pointer, IntegerType type) { - return makeIntegerCast( new PtrToIntCast(types.getIntegerType(((PointerType)pointer.getType()).getBitWidth()), pointer), type,false); + return new PtrToIntCast(type, pointer); } - public Expression makeIntToPtrCast(Expression operand, PointerType pointerType) { - return new IntToPtrCast(pointerType, makeIntegerCast(operand,types.getArchType(),false)); + // it does not make sense to cast to a pointer not of the arch size. + + private Expression makeIntToPtrCast(Expression integer, PointerType pointerType) { + return new IntToPtrCast(pointerType, integer); } public Expression makeIntToPtrCast(Expression operand) { return makeIntToPtrCast(operand, types.getPointerType()); @@ -426,8 +420,6 @@ public Expression makeIntToPtrCast(Expression operand) { - - public Expression makeNullLiteral(PointerType pointerType) { return new NullLiteral(pointerType); } @@ -486,7 +478,7 @@ else if (type instanceof IntegerType integerType) { } else if (type instanceof FloatType floatType) { return makeFloatCast(expression, floatType, signed); }else if (type instanceof PointerType) { - return makePtrCast(expression, (PointerType) type, false); // todo fix for tearing(mixed test), maybe a teared pointer tracker. + return makePtrCast(expression, (PointerType) type); // todo fix for tearing(mixed test), maybe a teared pointer tracker. } throw new UnsupportedOperationException(String.format("Cast %s into %s unsupported.", expression, type)); } @@ -519,10 +511,10 @@ public Expression makeEQ(Expression leftOperand, Expression rightOperand) { public Expression makeEQforced(Expression leftOperand, Expression rightOperand) { if (leftOperand.getType() instanceof PointerType){ - return makeEQforced(makePtrToIntCast(leftOperand), rightOperand); + return makeEQforced(makePtrToIntCast(leftOperand, types.getArchType()), rightOperand); } if (rightOperand.getType() instanceof PointerType){ - return makeEQforced(leftOperand, makePtrToIntCast(rightOperand)); + return makeEQforced(leftOperand, makePtrToIntCast(rightOperand, types.getArchType())); } return makeEQ(leftOperand, rightOperand); } @@ -547,10 +539,10 @@ public Expression makeNEQ(Expression leftOperand, Expression rightOperand) { public Expression makeNEQforced(Expression leftOperand, Expression rightOperand) { if (leftOperand.getType() instanceof PointerType){ - return makeNEQforced(makePtrToIntCast(leftOperand), rightOperand); + return makeNEQforced(makePtrToIntCast(leftOperand, types.getArchType()), rightOperand); } if (rightOperand.getType() instanceof PointerType){ - return makeNEQforced(leftOperand, makePtrToIntCast(rightOperand)); + return makeNEQforced(leftOperand, makePtrToIntCast(rightOperand, types.getArchType())); } return makeNEQ(leftOperand, rightOperand); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java index 091acfc341..3ee302f731 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/ExpressionVisitor.java @@ -65,7 +65,6 @@ public interface ExpressionVisitor { default TRet visitNullLiteral(NullLiteral lit) { return visitLeafExpression(lit); } default TRet visitPtrConcat(PtrConcat expr){ return visitExpression(expr); }; default TRet visitPtrExtract(PtrExtract expr){ return visitUnaryExpression(expr); }; - //default TRet visitPtrSizeCastExpression(PtrSizeCast expr){return visitCastExpression(expr); }; // =================================== Generic =================================== default TRet visitITEExpression(ITEExpr expr) { return visitExpression(expr); } diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java index 642a6ff24c..3ab7de8a08 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprSimplifier.java @@ -12,7 +12,6 @@ import com.dat3m.dartagnan.expression.integers.*; import com.dat3m.dartagnan.expression.misc.ITEExpr; import com.dat3m.dartagnan.expression.pointer.*; -import com.dat3m.dartagnan.expression.type.PointerType; import com.dat3m.dartagnan.expression.utils.IntegerHelper; import com.dat3m.dartagnan.program.memory.MemoryObject; import com.google.common.base.VerifyException; @@ -354,7 +353,7 @@ public Expression visitPtrAddExpression(PtrAddExpr expr) { } // can cause a tearing problem if (base instanceof NullLiteral) { - return expressions.makeIntToPtrCast(offset,expr.getType()); + return expressions.makeIntToPtrCast(offset); } return expressions.makePtrAdd(base, offset); } @@ -396,7 +395,7 @@ public Expression visitIntToPtrCastExpression(IntToPtrCast expr) { if (sub instanceof IntLiteral && ((IntLiteral) sub).isZero()) { return expressions.makeNullLiteral(expr.getType()); } - return expressions.makeIntToPtrCast(expr.getOperand(), expr.getType()); + return expressions.makeIntToPtrCast(expr.getOperand()); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java index cf780c6304..264037e1cc 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/processing/ExprTransformer.java @@ -139,7 +139,7 @@ public Expression visitPtrCmpExpression(PtrCmpExpr expr) { @Override public Expression visitPtrToIntCastExpression(PtrToIntCast expr) { - return expressions.makeIntegerCast(expressions.makePtrToIntCast(expr.getOperand().accept(this)), expr.getType(),false); + return expressions.makePtrToIntCast(expr.getOperand().accept(this), expr.getType()); } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java index 02cff5d9fa..dd012a468d 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/expression/type/PointerType.java @@ -7,7 +7,7 @@ public class PointerType implements Type { public int bitWidth; public boolean teared; - // todo change the encoding to recognise bit width + PointerType(int bitWidth) { this.bitWidth = bitWidth; @@ -15,7 +15,7 @@ public class PointerType implements Type { @Override public String toString() { - return "ptr_"+ bitWidth; + return "ptr"+ bitWidth; } @Override diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java index 94865645e9..c74a7fb04c 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/parsers/program/visitors/VisitorLitmusC.java @@ -603,7 +603,8 @@ public Object visitNreRegDeclaration(LitmusCParser.NreRegDeclarationContext ctx) Register register = programBuilder.getRegister(scope, ctx.varName().getText()); if(register == null){ - System.out.println(ctx.typeSpecifier().basicTypeSpecifier().getText()); + //todo remove printer + // System.out.println(ctx.typeSpecifier().basicTypeSpecifier().getText()); if (ctx.typeSpecifier().basicTypeSpecifier().getText().endsWith("*")){ register = programBuilder.getOrNewRegister(scope, ctx.varName().getText(), pointerType); }else{ diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java index e8516294a4..d4fa101781 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/Intrinsics.java @@ -62,6 +62,7 @@ public class Intrinsics { private enum AssertionType { USER, OVERFLOW, INVALIDDEREF, UNKNOWN_FUNCTION } private final boolean detectMixedSizeAccesses; + private final IntegerType archType = types.getArchType(); private static final TypeFactory types = TypeFactory.getInstance(); private static final ExpressionFactory expressions = ExpressionFactory.getInstance(); @@ -424,7 +425,7 @@ private List inlinePthreadCreate(FunctionCall call) { final Register resultRegister = getResultRegister(call); assert resultRegister.getType() instanceof IntegerType; - final Register tidReg = call.getFunction().newUniqueRegister("__tid", types.getArchType()); + final Register tidReg = call.getFunction().newUniqueRegister("__tid", archType); final Event createEvent = newDynamicThreadCreate(tidReg, PTHREAD_THREAD_TYPE, targetFunction, List.of(argument)); final Label skipAttrLabel = newLabel("__pthread_create_skip_attr"); final Label skipDetachLabel = newLabel("__pthread_create_skip_detach"); @@ -714,11 +715,11 @@ private List inlinePthreadKeyCreate(FunctionCall call) { final long threadCount = program.getThreads().size(); final int pointerBytes = types.getMemorySizeInBytes(types.getPointerType()); final Register storageAddressRegister = call.getFunction().newRegister(types.getPointerType()); - final Expression size = expressions.makeValue((threadCount + 1) * pointerBytes, types.getArchType()); - final Expression destructorOffset = expressions.makeValue(threadCount * pointerBytes, types.getArchType()); + final Expression size = expressions.makeValue((threadCount + 1) * pointerBytes, archType); + final Expression destructorOffset = expressions.makeValue(threadCount * pointerBytes, archType); //TODO call destructor at each thread's normal exit return List.of( - EventFactory.newAlloc(storageAddressRegister, types.getArchType(), size, true, true), + EventFactory.newAlloc(storageAddressRegister, archType, size, true, true), newStore(keyAddress, storageAddressRegister), newStore(expressions.makePtrAdd(storageAddressRegister, destructorOffset), destructor), assignSuccess(errorRegister) @@ -741,7 +742,7 @@ private List inlinePthreadGetSpecific(FunctionCall call) { final Register result = getResultRegisterAndCheckArguments(1, call); final Expression key = call.getArguments().get(0); final int threadID = call.getThread().getId(); - final Expression offset = expressions.makeValue(threadID, types.getArchType()); + final Expression offset = expressions.makeValue(threadID, archType); return List.of( EventFactory.newLoad(result, expressions.makePtrAdd(expressions.makeCast(key,types.getPointerType()), offset)) ); @@ -753,7 +754,7 @@ private List inlinePthreadSetSpecific(FunctionCall call) { final Expression key = call.getArguments().get(0); final Expression value = call.getArguments().get(1); final int threadID = call.getThread().getId(); - final Expression offset = expressions.makeValue(threadID, types.getArchType()); + final Expression offset = expressions.makeValue(threadID, archType); return List.of( newStore(expressions.makePtrAdd(expressions.makeCast(key,types.getPointerType()), offset), value), // this cast does not seem like a good idea todo discuss it assignSuccess(errorRegister) @@ -906,7 +907,7 @@ private List inlinePthreadRwlockDestroy(FunctionCall call) { final Register errorRegister = getResultRegisterAndCheckArguments(1, call); //TODO store a value such that later uses of the lock fail //final Expression lock = call.getArguments().get(0); - //final Expression finalizedValue = expressions.makeZero(types.getArchType()); + //final Expression finalizedValue = expressions.makeZero(archType); return List.of( //EventFactory.newStore(lock, finalizedValue) assignSuccess(errorRegister) @@ -1032,7 +1033,7 @@ private List inlinePthreadRwlockUnlock(FunctionCall call) { } private IntegerType getRwlockDatatype() { - return types.getArchType(); + return archType; } private IntLiteral getRwlockUnlockedValue() { @@ -1605,10 +1606,10 @@ private List inlineMemCpy(FunctionCall call) { final List replacement = new ArrayList<>(2 * count + 1); //FIXME without MSA detection, each byte is treated as a 64-bit value. - final IntegerType type = detectMixedSizeAccesses ? types.getIntegerType(8 * count) : types.getArchType(); + final IntegerType type = detectMixedSizeAccesses ? types.getIntegerType(8 * count) : archType; final int typeSize = detectMixedSizeAccesses ? count : 1; for (int i = 0; i < count; i += typeSize) { - final Expression offset = expressions.makeValue(i, types.getArchType()); + final Expression offset = expressions.makeValue(i, archType); final Expression srcAddr = expressions.makePtrAdd(src, offset); final Expression destAddr = expressions.makePtrAdd(dest, offset); final Register reg = caller.getOrNewRegister("__memcpy_" + i, type); @@ -1658,18 +1659,21 @@ private List inlineMemCpyS(FunctionCall call) { final Expression srcIsNull = expressions.makeEQ(src, nullExpr); // We assume RSIZE_MAX = 2^64-1 - final Expression rsize_max = expressions.makeValue(BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE), types.getArchType()); - // These parameters have type rsize_t/size_t which we model as types.getArchType(), thus the cast - final Expression castDestszExpr = expressions.makeCast(destszExpr, types.getArchType()); - final Expression castCountExpr = expressions.makeCast(countExpr, types.getArchType()); + final Expression rsize_max = expressions.makeValue(BigInteger.ONE.shiftLeft(64).subtract(BigInteger.ONE), archType); + // These parameters have type rsize_t/size_t which we model as archType, thus the cast + final Expression castDestszExpr = expressions.makeCast(destszExpr, archType); + final Expression castCountExpr = expressions.makeCast(countExpr, archType); final Expression invalidDestsz = expressions.makeGT(castDestszExpr, rsize_max, false); final Expression countGtMax = expressions.makeGT(castCountExpr, rsize_max, false); final Expression countGtdestszExpr = expressions.makeGT(castCountExpr, castDestszExpr, false); final Expression invalidCount = expressions.makeOr(countGtMax, countGtdestszExpr); final Expression overlap = expressions.makeAnd( - expressions.makeGT(expressions.makePtrToIntCast(expressions.makePtrAdd(src, castCountExpr)), expressions.makePtrToIntCast(dest), false), - expressions.makeGT(expressions.makePtrToIntCast(expressions.makePtrAdd(dest, castCountExpr)), expressions.makePtrToIntCast(src), false)); + expressions.makeGT(expressions.makePtrToIntCast(expressions.makePtrAdd(src, castCountExpr),archType), + expressions.makePtrToIntCast(dest, archType), false), + expressions.makeGT(expressions.makePtrToIntCast(expressions.makePtrAdd(dest, castCountExpr), archType), + expressions.makePtrToIntCast(src,archType), false)); + final List replacement = new ArrayList<>(); @@ -1705,9 +1709,9 @@ private List inlineMemCpyS(FunctionCall call) { skipE2 )); for (int i = 0; i < destsz; i++) { - final Expression offset = expressions.makeValue(i, types.getArchType()); + final Expression offset = expressions.makeValue(i, archType); final Expression destAddr = expressions.makePtrAdd(dest, offset); - final Expression zero = expressions.makeZero(types.getArchType()); + final Expression zero = expressions.makeZero(archType); replacement.add( newStore(destAddr, zero) ); @@ -1721,11 +1725,11 @@ private List inlineMemCpyS(FunctionCall call) { Local retSuccess = EventFactory.newLocal(resultRegister, errorCodeSuccess); replacement.add(success); for (int i = 0; i < count; i++) { - final Expression offset = expressions.makeValue(i, types.getArchType()); + final Expression offset = expressions.makeValue(i, archType); final Expression srcAddr = expressions.makePtrAdd(src, offset); final Expression destAddr = expressions.makePtrAdd(dest, offset); // FIXME: We have no other choice but to load ptr-sized chunks for now - final Register reg = caller.getOrNewRegister("__memcpy_" + i, types.getArchType()); + final Register reg = caller.getOrNewRegister("__memcpy_" + i, archType); replacement.addAll(List.of( EventFactory.newLoad(reg, srcAddr), @@ -1756,7 +1760,7 @@ private List inlineMemCmp(FunctionCall call) { final List replacement = new ArrayList<>(4 * count + 1); final Label endCmp = EventFactory.newLabel("__memcmp_end"); for (int i = 0; i < count; i++) { - final Expression offset = expressions.makeValue(i, types.getArchType()); + final Expression offset = expressions.makeValue(i, archType); final Expression src1Addr = expressions.makeAdd(src1, offset); final Expression src2Addr = expressions.makeAdd(src2, offset); //FIXME: This method should properly load byte chunks and compare them (unsigned). @@ -1808,7 +1812,7 @@ private List inlineMemSet(FunctionCall call) { final Expression zero = expressions.makeValue(fill, types.getByteType()); final List replacement = new ArrayList<>( count + 1); for (int i = 0; i < count; i++) { - final Expression offset = expressions.makeValue(i, types.getArchType()); + final Expression offset = expressions.makeValue(i, archType); final Expression destAddr = expressions.makePtrAdd(dest, offset); replacement.add(newStore(destAddr, zero)); diff --git a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java index 287a297eaf..feb5a38327 100644 --- a/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java +++ b/dartagnan/src/main/java/com/dat3m/dartagnan/program/processing/ThreadCreation.java @@ -185,7 +185,8 @@ private void resolveDynamicThreadJoin(Program program, List threadDa for (DynamicThreadJoin join : program.getThreadEvents(DynamicThreadJoin.class)) { final Thread caller = join.getThread(); final Expression tidExpr_ = join.getTid(); - final Expression tidExpr = tidExpr_.getType() instanceof PointerType ? expressions.makePtrToIntCast(tidExpr_) : tidExpr_; + // todo check if this makes sense as functions are now pointers + final Expression tidExpr = tidExpr_.getType() instanceof PointerType ? expressions.makePtrToIntCast(tidExpr_, archType) : tidExpr_; final Register joinRegister = join.getResultRegister(); final IntegerType statusType = (IntegerType) ((AggregateType)joinRegister.getType()).getFields().get(0).type(); diff --git a/dartagnan/src/test/java/com/dat3m/dartagnan/others/miscellaneous/AnalysisTest.java b/dartagnan/src/test/java/com/dat3m/dartagnan/others/miscellaneous/AnalysisTest.java index f441869e59..c9d698fe90 100644 --- a/dartagnan/src/test/java/com/dat3m/dartagnan/others/miscellaneous/AnalysisTest.java +++ b/dartagnan/src/test/java/com/dat3m/dartagnan/others/miscellaneous/AnalysisTest.java @@ -593,7 +593,7 @@ public void fullPropagation1() throws InvalidConfigurationException { Load e0 = newLoad(r0, y); // reads x b.addChild(0, e0); Label l0 = newLabel("l0"); - b.addChild(0, newJump(expressions.makeEQ(r0, expressions.makePtrToIntCast(x)), l0)); + b.addChild(0, newJump(expressions.makeEQ(r0, expressions.makePtrToIntCast(x, types.getArchType())), l0)); Load e1 = newLoad(r0, x); // reads y b.addChild(0, e1); b.addChild(0, l0);