// Copyright (c) 2014 Daniel Grunwald // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System.Diagnostics; using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.Decompiler.IL { /// /// Semantic meaning of a Conv instruction. /// public enum ConversionKind : byte { /// /// Invalid conversion. /// Invalid, /// /// Conversion between two types of same size. /// Can be used to change the sign of integer types, which may involve overflow-checking. /// Nop, /// /// Integer-to-float conversion. /// Uses InputSign to decide whether the integer should be treated as signed or unsigned. /// IntToFloat, /// /// Float-to-integer conversion. /// Truncates toward zero; may perform overflow-checking. /// FloatToInt, /// /// Converts from the current precision available on the evaluation stack to the precision specified by /// the TargetType. /// Uses "round-to-nearest" mode is the precision is reduced. /// FloatPrecisionChange, /// /// Conversion of integer type to larger signed integer type. /// May involve overflow checking (when converting from U4 to I on 32-bit). /// SignExtend, /// /// Conversion of integer type to larger unsigned integer type. /// May involve overflow checking (when converting from a signed type). /// ZeroExtend, /// /// Conversion to smaller integer type. /// /// May involve overflow checking. /// /// /// If the target type is smaller than the minimum stack width of 4 bytes, /// then the result of the conversion is zero extended (if the target type is unsigned) /// or sign-extended (if the target type is signed). /// Truncate, /// /// Used to convert managed references/objects to unmanaged pointers. /// StopGCTracking } partial class Conv : UnaryInstruction { /// /// Gets the conversion kind. /// public readonly ConversionKind Kind; /// /// The target type of the conversion. /// public readonly PrimitiveType TargetType; /// /// Gets whether the conversion performs overflow-checking. /// public readonly bool CheckForOverflow; /// /// Gets the sign of the input type. /// /// For conversions to integer types, the input Sign is set iff overflow-checking is enabled. /// For conversions to floating-point types, the input sign is always set. /// /// /// The input sign does not have any effect on whether the conversion zero-extends or sign-extends; /// that is purely determined by the TargetType. /// public readonly Sign InputSign; public Conv(ILInstruction argument, PrimitiveType targetType, bool checkForOverflow, Sign inputSign) : base(OpCode.Conv, argument) { this.TargetType = targetType; this.CheckForOverflow = checkForOverflow; this.InputSign = inputSign; Debug.Assert((inputSign != Sign.None) == (checkForOverflow || targetType == PrimitiveType.R4 || targetType == PrimitiveType.R8)); this.Kind = GetConversionKind(targetType, argument.ResultType, inputSign); Debug.Assert(this.Kind != ConversionKind.Invalid); } /// /// Implements Ecma-335 Table 8: Conversion Operators. /// static ConversionKind GetConversionKind(PrimitiveType targetType, StackType inputType, Sign inputSign) { switch (targetType) { case PrimitiveType.I1: case PrimitiveType.I2: case PrimitiveType.U1: case PrimitiveType.U2: switch (inputType) { case StackType.I4: case StackType.I8: case StackType.I: return ConversionKind.Truncate; case StackType.F: return ConversionKind.FloatToInt; default: return ConversionKind.Invalid; } case PrimitiveType.I4: case PrimitiveType.U4: switch (inputType) { case StackType.I4: return ConversionKind.Nop; case StackType.I: case StackType.I8: return ConversionKind.Truncate; case StackType.F: return ConversionKind.FloatToInt; default: return ConversionKind.Invalid; } case PrimitiveType.I8: case PrimitiveType.U8: switch (inputType) { case StackType.I4: case StackType.I: if (inputSign == Sign.None) return targetType == PrimitiveType.I8 ? ConversionKind.SignExtend : ConversionKind.ZeroExtend; else return inputSign == Sign.Signed ? ConversionKind.SignExtend : ConversionKind.ZeroExtend; case StackType.I8: return ConversionKind.Nop; case StackType.F: return ConversionKind.FloatToInt; case StackType.Ref: case StackType.O: return ConversionKind.StopGCTracking; default: return ConversionKind.Invalid; } case PrimitiveType.I: case PrimitiveType.U: switch (inputType) { case StackType.I4: if (inputSign == Sign.None) return targetType == PrimitiveType.I ? ConversionKind.SignExtend : ConversionKind.ZeroExtend; else return inputSign == Sign.Signed ? ConversionKind.SignExtend : ConversionKind.ZeroExtend; case StackType.I: return ConversionKind.Nop; case StackType.I8: return ConversionKind.Truncate; case StackType.F: return ConversionKind.FloatToInt; case StackType.Ref: case StackType.O: return ConversionKind.StopGCTracking; default: return ConversionKind.Invalid; } case PrimitiveType.R4: case PrimitiveType.R8: switch (inputType) { case StackType.I4: case StackType.I: case StackType.I8: return ConversionKind.IntToFloat; case StackType.F: return ConversionKind.FloatPrecisionChange; default: return ConversionKind.Invalid; } default: return ConversionKind.Invalid; } } public override StackType ResultType { get { return TargetType.GetStackType(); } } public override void WriteTo(ITextOutput output) { output.Write(OpCode); if (CheckForOverflow) output.Write(".ovf"); if (InputSign == Sign.Unsigned) output.Write(".unsigned"); else if (InputSign == Sign.Signed) output.Write(".signed"); output.Write(' '); output.Write(Argument.ResultType); output.Write("->"); output.Write(TargetType); output.Write(' '); switch (Kind) { case ConversionKind.SignExtend: output.Write(""); break; case ConversionKind.ZeroExtend: output.Write(""); break; case ConversionKind.Invalid: output.Write(""); break; } output.Write('('); Argument.WriteTo(output); output.Write(')'); } protected override InstructionFlags ComputeFlags() { var flags = base.ComputeFlags(); if (CheckForOverflow) flags |= InstructionFlags.MayThrow; return flags; } public override ILInstruction UnwrapConv(ConversionKind kind) { if (this.Kind == kind) return Argument.UnwrapConv(kind); else return this; } } }