示例#1
0
 private Statement ParseUnaryConditionalBranch(IOperation currentOperation)
 {
     Expression condition = this.PopOperandStack();
       var castIfPossible = condition as CastIfPossible;
       if (castIfPossible != null) {
     condition = new CheckIfInstance() {
       Locations = castIfPossible.Locations,
       Operand = castIfPossible.ValueToCast,
       TypeToCheck = castIfPossible.TargetType,
     };
       } else if (condition.Type != Dummy.TypeReference && condition.Type.TypeCode != PrimitiveTypeCode.Boolean) {
     var defaultValue = new DefaultValue() { DefaultValueType = condition.Type, Type = condition.Type };
     condition = new NotEquality() { LeftOperand = condition, RightOperand = defaultValue };
       }
       condition.Type = this.platformType.SystemBoolean;
       GotoStatement gotoStatement = MakeGoto(currentOperation);
       ConditionalStatement ifStatement = new ConditionalStatement();
       ifStatement.Condition = condition;
       switch (currentOperation.OperationCode) {
     case OperationCode.Brfalse:
     case OperationCode.Brfalse_S:
       ifStatement.TrueBranch = new EmptyStatement();
       ifStatement.FalseBranch = gotoStatement;
       break;
     case OperationCode.Brtrue:
     case OperationCode.Brtrue_S:
     default:
       ifStatement.TrueBranch = gotoStatement;
       ifStatement.FalseBranch = new EmptyStatement();
       break;
       }
       return ifStatement;
 }
示例#2
0
 /// <summary>
 /// Visits the specified default value.
 /// </summary>
 /// <param name="defaultValue">The default value.</param>
 public override void Visit(IDefaultValue defaultValue)
 {
     DefaultValue mutableDefaultValue = new DefaultValue(defaultValue);
     this.resultExpression = this.myCodeCopier.DeepCopy(mutableDefaultValue);
 }
示例#3
0
 /// <summary>
 /// Visits the specified default value.
 /// </summary>
 /// <param name="defaultValue">The default value.</param>
 /// <returns></returns>
 protected virtual IExpression DeepCopy(DefaultValue defaultValue)
 {
     defaultValue.DefaultValueType = Substitute(defaultValue.DefaultValueType);
       defaultValue.Type = this.Substitute(defaultValue.Type);
       return defaultValue;
 }
示例#4
0
    private static IExpression ConvertToBoolean(IExpression expression) {
      Contract.Requires(expression != null);
      Contract.Ensures(Contract.Result<IExpression>() != null);

      IPlatformType platformType = expression.Type.PlatformType;
      var cc = expression as CompileTimeConstant;
      if (cc != null && TypeHelper.IsPrimitiveInteger(cc.Type)) {
        cc.Value = !ExpressionHelper.IsIntegralZero(cc);
        cc.Type = platformType.SystemBoolean;
        return cc;
      }
      var conditional = expression as Conditional;
      if (conditional != null) {
        conditional.ResultIfTrue = ConvertToBoolean(conditional.ResultIfTrue);
        conditional.ResultIfFalse = ConvertToBoolean(conditional.ResultIfFalse);
        conditional.Type = platformType.SystemBoolean;
        return conditional;
      }
      object/*?*/ val = null;
      ITypeReference type = platformType.SystemObject;
      ITypeReference expressionType = expression.Type;
      IExpression rightOperand = null; // zero or null, but has to be type-specific
      switch (expressionType.TypeCode) {
        case PrimitiveTypeCode.Boolean: {
            var addrDeref = expression as AddressDereference;
            Conversion conversion;
            IManagedPointerTypeReference mgdPtr;
            if (addrDeref != null && (conversion = addrDeref.Address as Conversion) != null && 
              (mgdPtr = conversion.ValueToConvert.Type as IManagedPointerTypeReference) != null) {
              expressionType = mgdPtr.TargetType;
              addrDeref.Address = conversion.ValueToConvert;
              addrDeref.Type = expressionType;
              expression = addrDeref;
              goto default;
            }
            return expression;
          }
        case PrimitiveTypeCode.Char: val = (char)0; type = platformType.SystemChar; break;
        case PrimitiveTypeCode.Float32: val = (float)0; type = platformType.SystemFloat32; break;
        case PrimitiveTypeCode.Float64: val = (double)0; type = platformType.SystemFloat64; break;
        case PrimitiveTypeCode.Int16: val = (short)0; type = platformType.SystemInt16; break;
        case PrimitiveTypeCode.Int32: val = (int)0; type = platformType.SystemInt32; break;
        case PrimitiveTypeCode.Int64: val = (long)0; type = platformType.SystemInt64; break;
        case PrimitiveTypeCode.Int8: val = (sbyte)0; type = platformType.SystemInt8; break;
        case PrimitiveTypeCode.IntPtr: val = IntPtr.Zero; type = platformType.SystemIntPtr; break;
        case PrimitiveTypeCode.UInt16: val = (ushort)0; type = platformType.SystemUInt16; break;
        case PrimitiveTypeCode.UInt32: val = (uint)0; type = platformType.SystemUInt32; break;
        case PrimitiveTypeCode.UInt64: val = (ulong)0; type = platformType.SystemUInt64; break;
        case PrimitiveTypeCode.UInt8: val = (byte)0; type = platformType.SystemUInt8; break;
        case PrimitiveTypeCode.UIntPtr: val = UIntPtr.Zero; type = platformType.SystemUIntPtr; break;
        default:
          rightOperand = new DefaultValue() {
            DefaultValueType = expressionType,
            Type = expressionType,
          };
          break;
      }
      if (rightOperand == null) {
        rightOperand = new CompileTimeConstant() {
          Value = val,
          Type = type,
        };
      }
      NotEquality result = new NotEquality() {
        LeftOperand = expression,
        RightOperand = rightOperand,
        Type = platformType.SystemBoolean,
      };
      return result;
    }
    /// <summary>
    /// If the local declaration has an initial value, then generate the
    /// statement "loc := e" from it. 
    /// Special case: if "loc" is a struct, then treat it as a call to 
    /// the default ctor.
    /// Otherwise ignore it.
    /// </summary>
    public override void TraverseChildren(ILocalDeclarationStatement localDeclarationStatement) {
      var initVal = localDeclarationStatement.InitialValue;
      var typ = localDeclarationStatement.LocalVariable.Type;
      var isStruct = TranslationHelper.IsStruct(typ);
      if (initVal == null && !isStruct)
        return;
      var boogieLocal = this.sink.FindOrCreateLocalVariable(localDeclarationStatement.LocalVariable);
      var boogieLocalExpr = Bpl.Expr.Ident(boogieLocal);
      var tok = localDeclarationStatement.Token();
      Bpl.Expr e = null;
      

      var structCopy = isStruct && initVal != null && !(initVal is IDefaultValue);
      // then a struct value of type S is being assigned: "lhs := s"
      // model this as the statement "call lhs := S..#copy_ctor(s)" that does the bit-wise copying
      if (isStruct) {
        if (!structCopy) {
          var defaultValue = new DefaultValue() {
            DefaultValueType = typ,
            Locations = new List<ILocation>(localDeclarationStatement.Locations),
            Type = typ,
          };
          var e2 = ExpressionFor(defaultValue);
          StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok, boogieLocalExpr, e2));
        } else 
        /*if (structCopy) */{
          var proc = this.sink.FindOrCreateProcedureForStructCopy(typ);
          e = ExpressionFor(initVal);
          StmtBuilder.Add(new Bpl.CallCmd(tok, proc.Name, new List<Bpl.Expr> { e, }, new List<Bpl.IdentifierExpr>{ boogieLocalExpr, }));
        }
      } else {
        e = ExpressionFor(initVal);
        StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok, boogieLocalExpr, e));
      }
      return;
    }
示例#6
0
 /// <summary>
 /// Rewrites the children of the given defalut value expression.
 /// </summary>
 public virtual void RewriteChildren(DefaultValue defaultValue)
 {
     this.RewriteChildren((Expression)defaultValue);
       defaultValue.DefaultValueType = this.Rewrite(defaultValue.DefaultValueType);
 }
示例#7
0
 /// <summary>
 /// Visits the specified default value.
 /// </summary>
 /// <param name="defaultValue">The default value.</param>
 /// <returns></returns>
 public virtual IExpression Visit(DefaultValue defaultValue)
 {
     defaultValue.DefaultValueType = Visit(defaultValue.DefaultValueType);
       defaultValue.Type = this.Visit(defaultValue.Type);
       return defaultValue;
 }
示例#8
0
 /// <summary>
 /// Visits the specified default value.
 /// </summary>
 /// <param name="defaultValue">The default value.</param>
 public override void Visit(IDefaultValue defaultValue)
 {
     DefaultValue mutableDefaultValue = defaultValue as DefaultValue;
     if (alwaysMakeACopy || mutableDefaultValue == null) mutableDefaultValue = new DefaultValue(defaultValue);
     this.resultExpression = this.myCodeMutator.Visit(mutableDefaultValue);
 }