public override void TraverseChildren(IBlockStatement block) { base.TraverseChildren(block); Contract.Assume(block is BlockStatement); var decompiledBlock = (BlockStatement)block; var statements = decompiledBlock.Statements; for (int i = 0; i < statements.Count-1; i++) { var switchInstruction = statements[i] as SwitchInstruction; if (switchInstruction == null) continue; SwitchStatement result = new SwitchStatement(); result.Expression = switchInstruction.switchExpression; statements[i] = result; for (int j = 0, n = switchInstruction.SwitchCases.Count; j < n; j++) { CompileTimeConstant caseLabel = new CompileTimeConstant() { Value = j, Type = this.host.PlatformType.SystemInt32 }; var gotoCaseBody = switchInstruction.SwitchCases[j]; Contract.Assume(gotoCaseBody != null); SwitchCase currentCase = new SwitchCase() { Expression = caseLabel }; result.Cases.Add(currentCase); if (j < n-1) { Contract.Assume(switchInstruction.SwitchCases[j+1] != null); if (gotoCaseBody.TargetStatement == switchInstruction.SwitchCases[j+1].TargetStatement) continue; } currentCase.Body.Add(gotoCaseBody); } if (i == statements.Count-1) return; Contract.Assert(i+1 <= statements.Count); var gotoStatement = statements[i+1] as IGotoStatement; if (gotoStatement != null) { SwitchCase defaultCase = new SwitchCase() { }; // Default case is represented by a dummy Expression. defaultCase.Body.Add(statements[i + 1]); statements.RemoveAt(i + 1); result.Cases.Add(defaultCase); } } }
/// <summary> /// Visits the specified compile time constant. /// </summary> /// <param name="compileTimeConstant">The compile time constant.</param> /// <returns></returns> public virtual ICompileTimeConstant Visit(ICompileTimeConstant compileTimeConstant) { CompileTimeConstant mutableCompileTimeConstant = compileTimeConstant as CompileTimeConstant; if (!this.copyOnlyIfNotAlreadyMutable || mutableCompileTimeConstant == null) mutableCompileTimeConstant = new CompileTimeConstant(compileTimeConstant); return this.Visit(mutableCompileTimeConstant); }
private Expression ParseCompileTimeConstant(IOperation currentOperation) { Contract.Requires(currentOperation != null); CompileTimeConstant result = new CompileTimeConstant(); result.Value = currentOperation.Value; switch (currentOperation.OperationCode) { case OperationCode.Ldc_I4_0: result.Value = 0; break; case OperationCode.Ldc_I4_1: result.Value = 1; break; case OperationCode.Ldc_I4_2: result.Value = 2; break; case OperationCode.Ldc_I4_3: result.Value = 3; break; case OperationCode.Ldc_I4_4: result.Value = 4; break; case OperationCode.Ldc_I4_5: result.Value = 5; break; case OperationCode.Ldc_I4_6: result.Value = 6; break; case OperationCode.Ldc_I4_7: result.Value = 7; break; case OperationCode.Ldc_I4_8: result.Value = 8; break; case OperationCode.Ldc_I4_M1: result.Value = -1; break; } return result; }
private PointerIsValidationLocation(CompileTimeConstant pointerSize, IPrimarySourceLocation expressionLocation) : base(expressionLocation) { this.pointerSize = pointerSize; }
/// <summary> /// Visits the specified block. /// </summary> /// <param name="block">The block.</param> public override void Visit(IBlockStatement block) { if (this.insertAssumeFalseAtLine != null) { uint startLine; uint endLine; GetLocationLineSpan(GetPrimarySourceLocationFrom(block.Locations), out startLine, out endLine); if (startLine <= this.insertAssumeFalseAtLine.Value && this.insertAssumeFalseAtLine.Value < endLine) { foreach (IStatement stmt in block.Statements) { GetLocationLineSpan(GetPrimarySourceLocationFrom(stmt.Locations), out startLine, out endLine); GetPrimarySourceLocationFrom(stmt.Locations); if (this.insertAssumeFalseAtLine.Value < endLine) { AssumeStatement assumeFalse = new AssumeStatement(); CompileTimeConstant constFalse = new CompileTimeConstant(); constFalse.Value = false; assumeFalse.Condition = constFalse; this.GetOrCreateStmtListForStmt(stmt).Add(assumeFalse); break; } } } } base.Visit(block); }
private bool BooleanValueOfCompileTimeConstant(CompileTimeConstant ctc) { if (ctc.Type.TypeCode == PrimitiveTypeCode.Int32) return ((int)ctc.Value) != 0; if (ctc.Type.TypeCode == PrimitiveTypeCode.Boolean) return (bool)ctc.Value; throw new NotImplementedException("BooleanValueOfCompileTimeConstant: Unknown type of compile-time constant"); }
/// <summary> /// Visits the specified constant. /// </summary> /// <param name="constant">The constant.</param> public override void Visit(ICompileTimeConstant constant) { CompileTimeConstant mutableCompileTimeConstant = new CompileTimeConstant(constant); this.resultExpression = this.myCodeCopier.DeepCopy(mutableCompileTimeConstant); }
public override IExpression Visit(Conversion conversion) { conversion.ValueToConvert = this.Visit(conversion.ValueToConvert); if (TypeHelper.TypesAreEquivalent(conversion.TypeAfterConversion, conversion.ValueToConvert.Type) && // converting a floating point number to the same floating point number is not a nop: it might result in precision loss. !(conversion.TypeAfterConversion.TypeCode == PrimitiveTypeCode.Float32 || conversion.TypeAfterConversion.TypeCode == PrimitiveTypeCode.Float64) ) return conversion.ValueToConvert; else { var cc = conversion.ValueToConvert as CompileTimeConstant; if (cc != null) { if (cc.Value == null) { cc.Type = conversion.TypeAfterConversion; return cc; } if (conversion.TypeAfterConversion.TypeCode == PrimitiveTypeCode.Boolean && conversion.ValueToConvert.Type.TypeCode == PrimitiveTypeCode.Int32 && cc.Value is int) { var bcc = new CompileTimeConstant(); bcc.Value = ((int)cc.Value) != 0; bcc.Type = conversion.TypeAfterConversion; return bcc; } if (conversion.TypeAfterConversion.TypeCode == PrimitiveTypeCode.Char && conversion.ValueToConvert.Type.TypeCode == PrimitiveTypeCode.Int32 && cc.Value is int) { var bcc = new CompileTimeConstant(); bcc.Value = (char)(int)cc.Value; bcc.Type = conversion.TypeAfterConversion; return bcc; } } else if (conversion.TypeAfterConversion.TypeCode == PrimitiveTypeCode.Boolean) { var conditional = conversion.ValueToConvert as Conditional; if (conditional != null) { conditional.ResultIfFalse = this.ConvertToBoolean(conditional.ResultIfFalse); conditional.ResultIfTrue = this.ConvertToBoolean(conditional.ResultIfTrue); conditional.Type = conversion.TypeAfterConversion; return conditional; } } return conversion; } }
private void checkMethodCallForEventCancellation(IMethodCall call) { // NAVIGATION TODO this code is duplicated from PhoneNavigationTraverser, refactor that if (!call.MethodToCall.Name.Value.StartsWith("set_Cancel")) return; if (call.Arguments.Count() != 1 || call.Arguments.ToList()[0].Type != host.PlatformType.SystemBoolean) return; ICompileTimeConstant constant = call.Arguments.ToList()[0] as ICompileTimeConstant; if (constant != null && constant.Value != null) { CompileTimeConstant falseConstant = new CompileTimeConstant() { Type = host.PlatformType.SystemBoolean, Value = false, }; if (constant.Value == falseConstant.Value) return; } CancelsEvents = true; }
private static void AddArrayInitializers(CreateArray createArray, IFieldDefinition initialValueField, ulong[] sizes) { ITypeReference elemType = createArray.ElementType; MemoryStream memoryStream = new MemoryStream(new List<byte>(initialValueField.FieldMapping.Data).ToArray()); BinaryReader reader = new BinaryReader(memoryStream, Encoding.Unicode); ulong flatSize = 1; foreach (ulong dimensionSize in sizes) flatSize *= dimensionSize; while (flatSize-- > 0) { CompileTimeConstant cc = new CompileTimeConstant(); cc.Value = ReadValue(elemType.TypeCode, reader); cc.Type = elemType; createArray.Initializers.Add(cc); } }
private void DecompileSwitch(List<IStatement> statements, int i) { if (i >= statements.Count-1) return; SwitchInstruction/*?*/ switchInstruction = statements[i] as SwitchInstruction; if (switchInstruction == null) return; SwitchStatement result = new SwitchStatement(); result.Expression = switchInstruction.switchExpression; statements[i] = result; for (int j = 0, n = switchInstruction.switchCases.Count; j < n; j++) { CompileTimeConstant caseLabel = new CompileTimeConstant() { Value = j, Type = this.platformType.SystemInt32 }; var gotoCaseBody = switchInstruction.switchCases[j]; SwitchCase currentCase = new SwitchCase() { Expression = caseLabel }; result.Cases.Add(currentCase); if (j < n-1 && gotoCaseBody.TargetStatement == switchInstruction.switchCases[j+1].TargetStatement) continue; currentCase.Body.Add(gotoCaseBody); } if (i == statements.Count-1) return; var gotoStatement = statements[i+1] as IGotoStatement; if (gotoStatement != null) { SwitchCase defaultCase = new SwitchCase() { }; // Default case is represented by a dummy Expression. defaultCase.Body.Add(statements[i + 1]); statements.RemoveAt(i + 1); result.Cases.Add(defaultCase); } }
/// <summary> /// Rewrites the children of the given compile time constant. /// </summary> public virtual void RewriteChildren(CompileTimeConstant constant) { this.RewriteChildren((Expression)constant); }
/// <summary> /// Visits the specified constant. /// </summary> /// <param name="constant">The constant.</param> public override void Visit(ICompileTimeConstant constant) { CompileTimeConstant mutableCompileTimeConstant = constant as CompileTimeConstant; if (alwaysMakeACopy || mutableCompileTimeConstant == null) mutableCompileTimeConstant = new CompileTimeConstant(constant); this.resultExpression = this.myCodeMutator.Visit(mutableCompileTimeConstant); }
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; }
private void InitializeTraverser() { Microsoft.Cci.Immutable.PlatformType platform = host.PlatformType as Microsoft.Cci.Immutable.PlatformType; coreAssemblyRef = platform.CoreAssemblyRef; // TODO obtain version, culture and signature data dynamically AssemblyIdentity MSPhoneAssemblyId = new AssemblyIdentity(host.NameTable.GetNameFor("Microsoft.Phone"), "", new Version("7.0.0.0"), new byte[] { 0x24, 0xEE, 0xC0, 0xD8, 0xC8, 0x6C, 0xDA, 0x1E }, ""); AssemblyIdentity MSPhoneControlsAssemblyId= new AssemblyIdentity(host.NameTable.GetNameFor("Microsoft.Phone.Controls"), "", new Version("7.0.0.0"), new byte[] { 0x24, 0xEE, 0xC0, 0xD8, 0xC8, 0x6C, 0xDA, 0x1E }, ""); AssemblyIdentity MSPhoneSystemWindowsAssemblyId = new AssemblyIdentity(host.NameTable.GetNameFor("System.Windows"), coreAssemblyRef.Culture, coreAssemblyRef.Version, coreAssemblyRef.PublicKeyToken, ""); phoneAssembly = host.FindAssembly(MSPhoneAssemblyId); phoneSystemWindowsAssembly = host.FindAssembly(MSPhoneSystemWindowsAssemblyId); MSPhoneControlsAssembly= host.FindAssembly(MSPhoneControlsAssemblyId); // TODO BUG / XAML DEPENDENCE If a control is declared in XAML, it may be one from a library *not* linked! So, assemblies could be dummy here // TODO determine the needed types dynamically if (phoneAssembly != Dummy.Assembly) { appBarIconButtonType = platform.CreateReference(phoneAssembly, "Microsoft", "Phone", "Shell", "ApplicationBarIconButton"); appBarMenuItemType = platform.CreateReference(phoneAssembly, "Microsoft", "Phone", "Shell", "ApplicationBarMenuItem"); } else { appBarIconButtonType = host.PlatformType.SystemObject; appBarMenuItemType = host.PlatformType.SystemObject; } if (phoneSystemWindowsAssembly != Dummy.Assembly) { checkBoxType = platform.CreateReference(phoneSystemWindowsAssembly, "System", "Windows", "Controls", "CheckBox"); radioButtonType = platform.CreateReference(phoneSystemWindowsAssembly, "System", "Windows", "Controls", "RadioButton"); buttonType = platform.CreateReference(phoneSystemWindowsAssembly, "System", "Windows", "Controls", "Button"); buttonBaseType = platform.CreateReference(phoneSystemWindowsAssembly, "System", "Windows", "Controls", "Primitives", "ButtonBase"); toggleButtonType = platform.CreateReference(phoneSystemWindowsAssembly, "System", "Windows", "Controls", "Primitives", "ToggleButton"); controlType = platform.CreateReference(phoneSystemWindowsAssembly, "System", "Windows", "Controls", "Control"); uiElementType = platform.CreateReference(phoneSystemWindowsAssembly, "System", "Windows", "UIElement"); listBoxType = platform.CreateReference(phoneSystemWindowsAssembly, "System", "Windows", "Controls", "ListBox"); } else { checkBoxType = host.PlatformType.SystemObject; radioButtonType = host.PlatformType.SystemObject; buttonType = host.PlatformType.SystemObject; buttonBaseType = host.PlatformType.SystemObject; toggleButtonType = host.PlatformType.SystemObject; controlType = host.PlatformType.SystemObject; uiElementType = host.PlatformType.SystemObject; listBoxType = host.PlatformType.SystemObject; } if (MSPhoneControlsAssembly != Dummy.Assembly) { pivotType = platform.CreateReference(MSPhoneControlsAssembly, "Microsoft", "Phone", "Controls", "Pivot"); } else { pivotType = host.PlatformType.SystemObject; } trueConstant = new CompileTimeConstant() { Type = platform.SystemBoolean, Value = true }; falseConstant = new CompileTimeConstant() { Type = platform.SystemBoolean, Value = false }; IEnumerable<IPropertyDefinition> controlProperties = controlType.ResolvedType.Properties; IEnumerable<IPropertyDefinition> toggleButtonProperties = toggleButtonType.ResolvedType.Properties; IEnumerable<IPropertyDefinition> uiElementProperties = uiElementType.ResolvedType.Properties; IPropertyDefinition prop = controlProperties.Single(p => p.Name.Value == "IsEnabled"); isEnabledSetter = prop.Setter; isEnabledGetter = prop.Getter; prop = toggleButtonProperties.Single(p => p.Name.Value == "IsChecked"); isCheckedSetter = prop.Setter; isCheckedGetter = prop.Getter; prop = uiElementProperties.Single(p => p.Name.Value == "Visibility"); visibilitySetter = prop.Setter; visibilityGetter = prop.Getter; IEnumerable<IEventDefinition> buttonBaseEvents = buttonBaseType.ResolvedType.Events; IEnumerable<IEventDefinition> toggleButtonEvents = toggleButtonType.ResolvedType.Events; IEventDefinition evt = buttonBaseEvents.Single(e => e.Name.Value == "Click"); clickHandlerAdder = evt.Adder; clickHandlerRemover = evt.Remover; evt = toggleButtonEvents.Single(e => e.Name.Value == "Checked"); checkedHandlerAdder = evt.Adder; checkedHandlerRemover = evt.Remover; evt = toggleButtonEvents.Single(e => e.Name.Value == "Unchecked"); uncheckedHandlerAdder = evt.Adder; uncheckedHandlerRemover = evt.Remover; }
private static IExpression InvertCompileTimeConstant(CompileTimeConstant compileTimeConst) { Contract.Requires(compileTimeConst != null); Contract.Ensures(Contract.Result<IExpression>() != null); Contract.Assume(compileTimeConst.Value is bool); //since it is used as a condition, it is assumed to be a boolean return new CompileTimeConstant() { Value = !(bool)compileTimeConst.Value, Type = compileTimeConst.Type }; }
private bool ReplaceShortCircuitAnd6(BlockStatement b) { Contract.Requires(b != null); bool replacedPattern = false; var statements = b.Statements; for (int i = 0; i < statements.Count-8; i++) { var ifStatement = statements[i] as ConditionalStatement; if (ifStatement == null) continue; var gotoFalseCase = ifStatement.FalseBranch as GotoStatement; if (gotoFalseCase == null) continue; Contract.Assume(ifStatement.TrueBranch is EmptyStatement); var labeledStatement = statements[i+1] as LabeledStatement; if (labeledStatement == null) continue; var gotos = this.gotosThatTarget[(uint)labeledStatement.Label.UniqueKey]; if (gotos != null && gotos.Count > 0) continue; var ifStatement2 = statements[i+2] as ConditionalStatement; if (ifStatement2 == null) continue; var gotoTrueCase = ifStatement2.FalseBranch as GotoStatement; if (gotoTrueCase == null) continue; if (!(ifStatement2.TrueBranch is EmptyStatement)) continue; var labeledStatement2 = statements[i+3] as LabeledStatement; if (labeledStatement2 == null || labeledStatement2 != gotoFalseCase.TargetStatement) continue; gotos = this.gotosThatTarget[(uint)labeledStatement2.Label.UniqueKey]; Contract.Assume(gotos != null && gotos.Count > 0); if (gotos.Count > 1) continue; Contract.Assume(gotos[0] == gotoFalseCase); var pushFalseCase = statements[i+4] as PushStatement; if (pushFalseCase == null) continue; if (pushFalseCase.ValueToPush.Type.TypeCode != PrimitiveTypeCode.Boolean) continue; var gotoEnd = statements[i+5] as GotoStatement; if (gotoEnd == null) continue; var labeledStatement3 = statements[i+6] as LabeledStatement; if (labeledStatement3 == null || labeledStatement3 != gotoTrueCase.TargetStatement) continue; gotos = this.gotosThatTarget[(uint)labeledStatement3.Label.UniqueKey]; Contract.Assume(gotos != null && gotos.Count > 0); if (gotos.Count > 1) continue; Contract.Assume(gotos[0] == gotoTrueCase); var pushTrueCase = statements[i+7] as PushStatement; if (pushTrueCase == null) continue; if (pushTrueCase.ValueToPush.Type.TypeCode != PrimitiveTypeCode.Int32) continue; var pushTrueVal = pushTrueCase.ValueToPush as CompileTimeConstant; if (pushTrueVal == null || !(pushTrueVal.Value is int)) continue; if (1 != (int)pushTrueVal.Value) continue; var endLabel = statements[i+8] as LabeledStatement; if (endLabel == null || gotoEnd.TargetStatement != endLabel) continue; gotos = this.gotosThatTarget[(uint)endLabel.Label.UniqueKey]; Contract.Assume(gotos != null && gotos.Count > 0); if (gotos.Count > 1) continue; Contract.Assume(gotos[0] == gotoEnd); var falseConst = new CompileTimeConstant() { Value = false, Type = this.host.PlatformType.SystemBoolean }; var invertedCond1 = IfThenElseReplacer.InvertCondition(ifStatement2.Condition); var conditional1 = new Conditional() { Condition = ifStatement.Condition, ResultIfTrue = invertedCond1, ResultIfFalse = falseConst, Type = this.host.PlatformType.SystemBoolean }; var trueConst = new CompileTimeConstant() { Value = true, Type = this.host.PlatformType.SystemBoolean }; var invertedCond2 = IfThenElseReplacer.InvertCondition(pushFalseCase.ValueToPush); var conditional2 = new Conditional() { Condition = conditional1, ResultIfTrue = trueConst, ResultIfFalse = invertedCond2, Type = this.host.PlatformType.SystemBoolean }; pushTrueCase.ValueToPush = conditional2; statements.RemoveAt(i+8); statements.RemoveRange(i, 7); replacedPattern = true; } return replacedPattern; }
/// <summary> /// Tries to extarct the value of a named argument and make it an expression /// </summary> /// <param name="attribute"></param> /// <param name="argumentName"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static bool TryGetNamedArgumentValue( ICustomAttribute attribute, string argumentName, out CompileTimeConstant defaultValue) { Contract.Requires(attribute != null); Contract.Requires(!String.IsNullOrEmpty(argumentName)); foreach(var namedArgument in attribute.NamedArguments) { if (String.Equals(namedArgument.ArgumentName.Value, argumentName, StringComparison.Ordinal)) { var valueExpression = namedArgument.ArgumentValue; var value = valueExpression as IMetadataConstant; if (value != null) { defaultValue = new CompileTimeConstant { Type = value.Type, Value = value.Value }; // good candidate return true; } } } defaultValue = null; return false; }
static void AddSizesAndArrayInitializers(CreateArray createArray, IFieldDefinition initialValueField) { Contract.Requires(createArray != null); Contract.Requires(initialValueField != null); Contract.Requires(initialValueField.IsMapped); List<ulong> sizes = new List<ulong>(); foreach (IExpression expr in createArray.Sizes) { IMetadataConstant mdc = expr as IMetadataConstant; if (mdc == null) return; sizes.Add(ConvertToUlong(mdc)); } ITypeReference elemType = createArray.ElementType; MemoryStream memoryStream = new MemoryStream(new List<byte>(initialValueField.FieldMapping.Data).ToArray()); BinaryReader reader = new BinaryReader(memoryStream, Encoding.Unicode); ulong flatSize = 1; foreach (ulong dimensionSize in sizes) flatSize *= dimensionSize; while (flatSize-- > 0) { CompileTimeConstant cc = new CompileTimeConstant(); cc.Value = ReadValue(elemType.TypeCode, reader); cc.Type = elemType; createArray.Initializers.Add(cc); } }
/// <summary> /// Visits the specified constant. /// </summary> /// <param name="constant">The constant.</param> /// <returns></returns> protected virtual ICompileTimeConstant DeepCopy(CompileTimeConstant constant) { constant.Type = this.Substitute(constant.Type); return constant; }
private bool ReplaceShortCircuitAnd(BlockStatement b) { Contract.Requires(b != null); bool replacedPattern = false; var statements = b.Statements; for (int i = 0; i < statements.Count-6; i++) { var ifStatement = statements[i] as ConditionalStatement; if (ifStatement == null) continue; var gotoFalseCase = ifStatement.FalseBranch as GotoStatement; if (gotoFalseCase == null) continue; Contract.Assume(ifStatement.TrueBranch is EmptyStatement); var labeledStatement = statements[i+1] as LabeledStatement; if (labeledStatement == null) continue; var gotos = this.gotosThatTarget[(uint)labeledStatement.Label.UniqueKey]; if (gotos != null && gotos.Count > 0) continue; var pushTrueCase = statements[i+2] as PushStatement; if (pushTrueCase == null) continue; if (pushTrueCase.ValueToPush.Type.TypeCode != PrimitiveTypeCode.Boolean) continue; var gotoEnd = statements[i+3] as GotoStatement; if (gotoEnd == null) continue; var labeledStatement2 = statements[i+4] as LabeledStatement; if (labeledStatement2 == null || labeledStatement2 != gotoFalseCase.TargetStatement) continue; gotos = this.gotosThatTarget[(uint)labeledStatement2.Label.UniqueKey]; Contract.Assume(gotos != null && gotos.Count > 0); if (gotos.Count > 1) continue; var pushFalseCase = statements[i+5] as PushStatement; if (pushFalseCase == null) continue; var falseCaseVal = pushFalseCase.ValueToPush as CompileTimeConstant; if (falseCaseVal == null || !(falseCaseVal.Value is int)) continue; var endLabel = statements[i+6] as LabeledStatement; if (endLabel == null || gotoEnd.TargetStatement != endLabel) continue; Conditional conditional; if (((int)falseCaseVal.Value) != 0) { var trueConst = new CompileTimeConstant() { Value = true, Type = this.host.PlatformType.SystemBoolean }; var cond = new LogicalNot() { Operand = ifStatement.Condition, Type = this.host.PlatformType.SystemBoolean }; conditional = new Conditional() { Condition = cond, ResultIfTrue = trueConst, ResultIfFalse = pushTrueCase.ValueToPush, Type = this.host.PlatformType.SystemBoolean }; } else { var falseConst = new CompileTimeConstant() { Value = false, Type = this.host.PlatformType.SystemBoolean }; conditional = new Conditional() { Condition = ifStatement.Condition, ResultIfTrue = pushTrueCase.ValueToPush, ResultIfFalse = falseConst, Type = this.host.PlatformType.SystemBoolean }; } pushFalseCase.ValueToPush = conditional; statements.RemoveAt(i+6); statements.RemoveRange(i, 5); replacedPattern = true; } return replacedPattern; }
private bool isEventCancellationMethodCall(IMethodCall call) { if (!call.MethodToCall.Name.Value.StartsWith("set_Cancel")) return false; if (call.Arguments.Count() != 1 || call.Arguments.ToList()[0].Type != host.PlatformType.SystemBoolean) return false; if (call.ThisArgument == null || !call.ThisArgument.Type.isCancelEventArgsClass(host)) return false; ICompileTimeConstant constant = call.Arguments.ToList()[0] as ICompileTimeConstant; if (constant != null && constant.Value != null) { CompileTimeConstant falseConstant = new CompileTimeConstant() { Type = host.PlatformType.SystemBoolean, Value = false, }; if (constant.Value == falseConstant.Value) return false; } return true; }
private bool ReplaceShortCircuitAnd2(BlockStatement b) { Contract.Requires(b != null); bool replacedPattern = false; var statements = b.Statements; for (int i = 0; i < statements.Count-3; i++) { var ifStatement = statements[i] as ConditionalStatement; if (ifStatement == null) continue; var gotoFalseCase = ifStatement.FalseBranch as GotoStatement; if (gotoFalseCase == null) continue; Contract.Assume(ifStatement.TrueBranch is EmptyStatement); var labeledStatement = statements[i+1] as LabeledStatement; if (labeledStatement == null) continue; var gotos = this.gotosThatTarget[(uint)labeledStatement.Label.UniqueKey]; if (gotos != null && gotos.Count > 0) continue; var ifStatement2 = statements[i+2] as ConditionalStatement; if (ifStatement2 == null) continue; var gotoTrueCase = ifStatement2.TrueBranch as GotoStatement; if (gotoTrueCase == null) continue; Contract.Assume(ifStatement2.FalseBranch is EmptyStatement); var labeledStatement2 = statements[i+3] as LabeledStatement; if (labeledStatement2 == null) continue; if (labeledStatement2 == gotoFalseCase.TargetStatement) { gotos = this.gotosThatTarget[(uint)labeledStatement2.Label.UniqueKey]; Contract.Assume(gotos != null && gotos.Count > 0); if (gotos.Count > 1) continue; var falseConst = new CompileTimeConstant() { Value = false, Type = this.host.PlatformType.SystemBoolean }; var conditional = new Conditional() { Condition = ifStatement.Condition, ResultIfTrue = ifStatement2.Condition, ResultIfFalse = falseConst, Type = this.host.PlatformType.SystemBoolean }; ifStatement2.Condition = conditional; statements.RemoveRange(i, 2); gotos.Remove(gotoFalseCase); replacedPattern = true; } else { if (gotoFalseCase.TargetStatement != gotoTrueCase.TargetStatement) continue; //actually have a short circuit or here gotos = this.gotosThatTarget[(uint)gotoFalseCase.TargetStatement.Label.UniqueKey]; Contract.Assume(gotos != null && gotos.Count > 0); var trueConst = new CompileTimeConstant() { Value = true, Type = this.host.PlatformType.SystemBoolean }; var invertedCond = new LogicalNot() { Operand = ifStatement.Condition, Type = this.host.PlatformType.SystemBoolean }; var conditional = new Conditional() { Condition = invertedCond, ResultIfTrue = trueConst, ResultIfFalse = ifStatement2.Condition, Type = this.host.PlatformType.SystemBoolean }; ifStatement2.Condition = conditional; statements.RemoveRange(i, 2); gotos.Remove(gotoFalseCase); replacedPattern = true; } } return replacedPattern; }
private MethodCall GetPointerValidationCall(IExpression pointer) { CompileTimeConstant pointerSize = new CompileTimeConstant(); pointerSize.Type = pointer.Type.PlatformType.SystemInt32.ResolvedType; pointerSize.Value = pointer.Type.PlatformType.PointerSize; MethodCall mcall = new MethodCall(); mcall.Arguments.Add(pointer); mcall.Arguments.Add(pointerSize); mcall.Locations.Add(PointerIsValidationLocation.For(pointerSize, pointer.Locations)); mcall.MethodToCall = this.PointerValidator; mcall.Type = this.PointerValidator.Type; return mcall; }
private bool ReplaceShortCircuitAnd4(BlockStatement b) { Contract.Requires(b != null); bool replacedPattern = false; var statements = b.Statements; for (int i = 0; i < statements.Count-2; i++) { var ifStatement1 = statements[i] as ConditionalStatement; if (ifStatement1 == null || !(ifStatement1.FalseBranch is EmptyStatement)) continue; var goto1 = ifStatement1.TrueBranch as GotoStatement; if (goto1 == null) continue; var labStat = statements[i+1] as LabeledStatement; if (labStat == null) continue; var gotos1 = this.gotosThatTarget[(uint)labStat.Label.UniqueKey]; if (gotos1 != null && gotos1.Count > 0) continue; var ifStatement2 = statements[i+2] as ConditionalStatement; if (ifStatement2 == null) continue; if (ifStatement2.TrueBranch is EmptyStatement) { var goto2 = ifStatement2.FalseBranch as GotoStatement; if (goto2 == null) continue; if (goto1.TargetStatement != goto2.TargetStatement) continue; var gotos = this.gotosThatTarget[(uint)goto1.TargetStatement.Label.UniqueKey]; if (gotos == null || gotos.Count == 0) continue; var falseConst = new CompileTimeConstant() { Value = false, Type = this.host.PlatformType.SystemBoolean }; var notCond1 = new LogicalNot() { Operand = ifStatement1.Condition, Type = this.host.PlatformType.SystemBoolean }; var conditional = new Conditional() { Condition = notCond1, ResultIfTrue = ifStatement2.Condition, ResultIfFalse = falseConst, Type = this.host.PlatformType.SystemBoolean }; ifStatement2.Condition = conditional; statements.RemoveRange(i, 2); gotos.Remove(goto1); replacedPattern = true; } else if (ifStatement2.FalseBranch is EmptyStatement) { var goto2 = ifStatement2.TrueBranch as GotoStatement; if (goto2 == null) continue; if (goto1.TargetStatement != goto2.TargetStatement) continue; var gotos = this.gotosThatTarget[(uint)goto1.TargetStatement.Label.UniqueKey]; if (gotos == null || gotos.Count == 0) continue; var trueConst = new CompileTimeConstant() { Value = true, Type = this.host.PlatformType.SystemBoolean }; var cond1 = ifStatement1.Condition; var conditional = new Conditional() { Condition = cond1, ResultIfTrue = trueConst, ResultIfFalse = ifStatement2.Condition, Type = this.host.PlatformType.SystemBoolean }; ifStatement2.Condition = conditional; statements.RemoveRange(i, 2); gotos.Remove(goto1); replacedPattern = true; } } return replacedPattern; }
internal static PointerIsValidationLocation For(CompileTimeConstant pointerSize, IEnumerable<ILocation> locations) { IPrimarySourceLocation ploc = AssertAssumeAdderVisitor.GetPrimarySourceLocationFrom(locations); return new PointerIsValidationLocation(pointerSize, ploc); }
private bool ReplaceShortCircuitAnd5(BlockStatement b) { Contract.Requires(b != null); bool replacedPattern = false; var statements = b.Statements; for (int i = 0; i < statements.Count; i++) { var ifStatement = statements[i] as ConditionalStatement; if (ifStatement == null) continue; var pushTrue = ifStatement.TrueBranch as IPushStatement; if (pushTrue == null) { var trueBlock = ifStatement.TrueBranch as BlockStatement; if (trueBlock != null && trueBlock.Statements.Count == 1) pushTrue = trueBlock.Statements[0] as IPushStatement; } if (pushTrue == null || pushTrue.ValueToPush.Type.TypeCode != PrimitiveTypeCode.Boolean) continue; var pushFalse = ifStatement.FalseBranch as IPushStatement; if (pushFalse == null) { var falseBlock = ifStatement.FalseBranch as BlockStatement; if (falseBlock != null && falseBlock.Statements.Count == 1) pushFalse = falseBlock.Statements[0] as IPushStatement; } if (pushFalse == null) continue; var falseCaseVal = pushFalse.ValueToPush as CompileTimeConstant; if (falseCaseVal == null || !(falseCaseVal.Value is int)) continue; if (0 != (int)falseCaseVal.Value) continue; var falseConst = new CompileTimeConstant() { Value = false, Type = this.host.PlatformType.SystemBoolean }; statements[i] = new PushStatement() { ValueToPush = new Conditional() { Condition = ifStatement.Condition, ResultIfTrue = pushTrue.ValueToPush, ResultIfFalse = falseConst, Type = pushTrue.ValueToPush.Type } }; replacedPattern = true; } return replacedPattern; }
private Expression ParseCompileTimeConstant(IOperation currentOperation) { CompileTimeConstant result = new CompileTimeConstant(); result.Value = currentOperation.Value; result.Type = this.platformType.SystemInt32; switch (currentOperation.OperationCode) { case OperationCode.Ldc_I4_0: result.Value = 0; break; case OperationCode.Ldc_I4_1: result.Value = 1; break; case OperationCode.Ldc_I4_2: result.Value = 2; break; case OperationCode.Ldc_I4_3: result.Value = 3; break; case OperationCode.Ldc_I4_4: result.Value = 4; break; case OperationCode.Ldc_I4_5: result.Value = 5; break; case OperationCode.Ldc_I4_6: result.Value = 6; break; case OperationCode.Ldc_I4_7: result.Value = 7; break; case OperationCode.Ldc_I4_8: result.Value = 8; break; case OperationCode.Ldc_I4_M1: result.Value = -1; break; case OperationCode.Ldc_I8: result.Type = this.platformType.SystemInt64; break; case OperationCode.Ldc_R4: result.Type = this.platformType.SystemFloat32; break; case OperationCode.Ldc_R8: result.Type = this.platformType.SystemFloat64; break; case OperationCode.Ldnull: result.Type = this.platformType.SystemObject; break; case OperationCode.Ldstr: result.Type = this.platformType.SystemString; break; } return result; }
/// <summary> /// Visits the specified constant. /// </summary> /// <param name="constant">The constant.</param> /// <returns></returns> public virtual ICompileTimeConstant Visit(CompileTimeConstant constant) { constant.Type = this.Visit(constant.Type); return constant; }