Ejemplo n.º 1
0
 public void oldConvertPointerToInt(
     final ILocation loc, final ExpressionResult rexp, final CPrimitive newType) {
   assert newType.isIntegerType();
   assert rexp.lrVal.getCType() instanceof CPointer;
   if (OVERAPPROXIMATE_INT_POINTER_CONVERSION) {
     super.convertPointerToInt(loc, rexp, newType);
   } else {
     final Expression pointerExpression = rexp.lrVal.getValue();
     final Expression intExpression;
     if (mTypeSizes.useFixedTypeSizes()) {
       final BigInteger maxPtrValuePlusOne = mTypeSizes.getMaxValueOfPointer().add(BigInteger.ONE);
       final IntegerLiteral maxPointer = new IntegerLiteral(loc, maxPtrValuePlusOne.toString());
       intExpression =
           constructArithmeticExpression(
               loc,
               IASTBinaryExpression.op_plus,
               constructArithmeticExpression(
                   loc,
                   IASTBinaryExpression.op_multiply,
                   MemoryHandler.getPointerBaseAddress(pointerExpression, loc),
                   newType,
                   maxPointer,
                   newType),
               newType,
               MemoryHandler.getPointerOffset(pointerExpression, loc),
               newType);
     } else {
       intExpression = MemoryHandler.getPointerOffset(pointerExpression, loc);
     }
     final RValue rValue = new RValue(intExpression, newType, false, true);
     rexp.lrVal = rValue;
   }
 }
Ejemplo n.º 2
0
 @Override
 public void convertIntToFloat(
     final ILocation loc, final ExpressionResult rexp, final CPrimitive newType) {
   final String prefixedFunctionName =
       declareConversionFunction(loc, (CPrimitive) rexp.lrVal.getCType(), newType);
   final Expression oldExpression = rexp.lrVal.getValue();
   final Expression resultExpression =
       new FunctionApplication(loc, prefixedFunctionName, new Expression[] {oldExpression});
   final RValue rValue = new RValue(resultExpression, newType, false, false);
   rexp.lrVal = rValue;
 }
Ejemplo n.º 3
0
 public void oldConvertIntToPointer(
     final ILocation loc, final ExpressionResult rexp, final CPointer newType) {
   if (OVERAPPROXIMATE_INT_POINTER_CONVERSION) {
     super.convertIntToPointer(loc, rexp, newType);
   } else {
     final Expression intExpression = rexp.lrVal.getValue();
     final Expression baseAdress;
     final Expression offsetAdress;
     if (mTypeSizes.useFixedTypeSizes()) {
       final BigInteger maxPtrValuePlusOne = mTypeSizes.getMaxValueOfPointer().add(BigInteger.ONE);
       final IntegerLiteral maxPointer = new IntegerLiteral(loc, maxPtrValuePlusOne.toString());
       baseAdress =
           constructArithmeticExpression(
               loc,
               IASTBinaryExpression.op_divide,
               intExpression,
               getCTypeOfPointerComponents(),
               maxPointer,
               getCTypeOfPointerComponents());
       offsetAdress =
           constructArithmeticExpression(
               loc,
               IASTBinaryExpression.op_modulo,
               intExpression,
               getCTypeOfPointerComponents(),
               maxPointer,
               getCTypeOfPointerComponents());
     } else {
       baseAdress =
           constructLiteralForIntegerType(loc, getCTypeOfPointerComponents(), BigInteger.ZERO);
       offsetAdress = intExpression;
     }
     final Expression pointerExpression =
         MemoryHandler.constructPointerFromBaseAndOffset(baseAdress, offsetAdress, loc);
     final RValue rValue = new RValue(pointerExpression, newType, false, false);
     rexp.lrVal = rValue;
   }
 }
Ejemplo n.º 4
0
  private void convertToIntegerType(
      final ILocation loc, final ExpressionResult operand, final CPrimitive resultType) {
    assert resultType.isIntegerType();
    final CPrimitive oldType = (CPrimitive) operand.lrVal.getCType();
    if (oldType.isIntegerType()) {
      final Expression newExpression;
      if (mTypeSizes.isUnsigned(resultType)) {
        final Expression oldWrappedIfNeeded;
        if (mTypeSizes.isUnsigned(oldType)
            && mTypeSizes.getSize(resultType.getType()) > mTypeSizes.getSize(oldType.getType())) {
          // required for sound Nutz transformation
          // (see examples/programs/regression/c/NutzTransformation03.c)
          oldWrappedIfNeeded = applyWraparound(loc, mTypeSizes, oldType, operand.lrVal.getValue());
        } else {
          oldWrappedIfNeeded = operand.lrVal.getValue();
        }
        if (mUnsignedTreatment == UnsignedTreatment.ASSERT) {
          final BigInteger maxValuePlusOne =
              mTypeSizes.getMaxValueOfPrimitiveType(resultType).add(BigInteger.ONE);
          final AssertStatement assertGeq0 =
              new AssertStatement(
                  loc,
                  ExpressionFactory.newBinaryExpression(
                      loc,
                      BinaryExpression.Operator.COMPGEQ,
                      oldWrappedIfNeeded,
                      new IntegerLiteral(loc, SFO.NR0)));
          final Check chk1 = new Check(Spec.UINT_OVERFLOW);
          chk1.addToNodeAnnot(assertGeq0);
          operand.stmt.add(assertGeq0);

          final AssertStatement assertLtMax =
              new AssertStatement(
                  loc,
                  ExpressionFactory.newBinaryExpression(
                      loc,
                      BinaryExpression.Operator.COMPLT,
                      oldWrappedIfNeeded,
                      new IntegerLiteral(loc, maxValuePlusOne.toString())));
          final Check chk2 = new Check(Spec.UINT_OVERFLOW);
          chk2.addToNodeAnnot(assertLtMax);
          operand.stmt.add(assertLtMax);
        } else {
          // do nothing
        }
        newExpression = oldWrappedIfNeeded;
      } else {
        assert !mTypeSizes.isUnsigned(resultType);
        final Expression oldWrappedIfUnsigned;
        if (mTypeSizes.isUnsigned(oldType)) {
          // required for sound Nutz transformation
          // (see examples/programs/regression/c/NutzTransformation01.c)
          oldWrappedIfUnsigned =
              applyWraparound(loc, mTypeSizes, oldType, operand.lrVal.getValue());
        } else {
          oldWrappedIfUnsigned = operand.lrVal.getValue();
        }
        if (mTypeSizes.getSize(resultType.getType()) > mTypeSizes.getSize(oldType.getType())
            || (mTypeSizes
                    .getSize(resultType.getType())
                    .equals(mTypeSizes.getSize(oldType.getType()))
                && !mTypeSizes.isUnsigned(oldType))) {
          newExpression = oldWrappedIfUnsigned;
        } else {
          // According to C11 6.3.1.3.3 the result is implementation-defined
          // it the value cannot be represented by the new type
          // We have chosen an implementation that is similar to
          // taking the lowest bits in a two's complement representation:
          // First we take the value modulo the cardinality of the
          // data range (which is 2*(MAX_VALUE+1) for signed )
          // If the number is strictly larger than MAX_VALUE we
          // subtract the cardinality of the data range.
          final CPrimitive correspondingUnsignedType =
              mTypeSizes.getCorrespondingUnsignedType(resultType);
          final Expression wrapped =
              applyWraparound(loc, mTypeSizes, correspondingUnsignedType, oldWrappedIfUnsigned);
          final Expression maxValue =
              constructLiteralForIntegerType(
                  loc, oldType, mTypeSizes.getMaxValueOfPrimitiveType(resultType));
          final Expression condition =
              ExpressionFactory.newBinaryExpression(loc, Operator.COMPLEQ, wrapped, maxValue);
          final Expression range =
              constructLiteralForIntegerType(
                  loc,
                  oldType,
                  mTypeSizes
                      .getMaxValueOfPrimitiveType(correspondingUnsignedType)
                      .add(BigInteger.ONE));
          newExpression =
              ExpressionFactory.newIfThenElseExpression(
                  loc,
                  condition,
                  wrapped,
                  ExpressionFactory.newBinaryExpression(loc, Operator.ARITHMINUS, wrapped, range));
        }
      }
      final RValue newRValue = new RValue(newExpression, resultType, false, false);
      operand.lrVal = newRValue;
    } else {
      throw new UnsupportedOperationException("not yet supported: conversion from " + oldType);
    }
  }
Ejemplo n.º 5
0
 @Override
 public void convertFloatToFloat(
     final ILocation loc, final ExpressionResult rexp, final CPrimitive newType) {
   final RValue oldRValue = (RValue) rexp.lrVal;
   rexp.lrVal = new RValue(oldRValue.getValue(), newType);
 }