Search in sources :

Example 6 with IntExpr

use of com.microsoft.z3.IntExpr in project spoon by INRIA.

the class CommonUtils method getTargetValue.

/**
 * Gets target expression value by going from left to right.
 * For example, 'a.b' is calculated as follows: memory[T2.b][memory[T1.a][a]]
 */
public static IntExpr getTargetValue(Context context, Map<CtReference, Expr> variablesMap, Memory memory, CtExpression<?> target) {
    Deque<CtExpression> targets = new ArrayDeque<>();
    while (target instanceof CtTargetedExpression) {
        targets.addFirst(target);
        target = ((CtTargetedExpression) target).getTarget();
    }
    targets.addFirst(target);
    // Traverse all targets left to right
    IntExpr targetValue = null;
    for (CtExpression t : targets) {
        if (t instanceof CtFieldRead) {
            targetValue = (IntExpr) memory.read(((CtFieldRead) t).getVariable(), targetValue);
        } else if (t instanceof CtArrayRead) {
            CtArrayRead arrayRead = (CtArrayRead) t;
            CtExpression index = arrayRead.getIndexExpression();
            Expr arrayIndex = (Expr) index.getMetadata("value");
            targetValue = (IntExpr) memory.readArray((CtArrayTypeReference) arrayRead.getTarget().getType(), targetValue, arrayIndex);
        } else if (t instanceof CtVariableRead) {
            targetValue = (IntExpr) variablesMap.get(((CtVariableRead) t).getVariable());
        } else if (t instanceof CtTypeAccess) {
            targetValue = (IntExpr) variablesMap.get(TypeUtils.getActualType(t));
            if (targetValue == null) {
                targetValue = (IntExpr) context.mkFreshConst("", context.getIntSort());
                variablesMap.put(TypeUtils.getActualType(t), targetValue);
            }
        } else if (t instanceof CtThisAccess) {
            targetValue = context.mkInt(Memory.thisPointer());
        } else {
            // Impure functions and other unknown stuff
            targetValue = (IntExpr) context.mkFreshConst("", context.getIntSort());
        }
    }
    return targetValue;
}
Also used : IntExpr(com.microsoft.z3.IntExpr) Expr(com.microsoft.z3.Expr) IntExpr(com.microsoft.z3.IntExpr) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) ArrayDeque(java.util.ArrayDeque)

Aggregations

Expr (com.microsoft.z3.Expr)3 BitVecExpr (com.microsoft.z3.BitVecExpr)2 BoolExpr (com.microsoft.z3.BoolExpr)2 IntExpr (com.microsoft.z3.IntExpr)2 Set (java.util.Set)2 com.microsoft.z3 (com.microsoft.z3)1 ArithExpr (com.microsoft.z3.ArithExpr)1 ComputeInterpolantResult (com.microsoft.z3.InterpolationContext.ComputeInterpolantResult)1 Event (dartagnan.program.Event)1 HighLocation (dartagnan.program.HighLocation)1 If (dartagnan.program.If)1 Init (dartagnan.program.Init)1 Load (dartagnan.program.Load)1 Local (dartagnan.program.Local)1 Location (dartagnan.program.Location)1 MemEvent (dartagnan.program.MemEvent)1 Program (dartagnan.program.Program)1 Register (dartagnan.program.Register)1 Store (dartagnan.program.Store)1 MapSSA (dartagnan.utils.MapSSA)1