#nullable enable // 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 if 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, /// /// Used to convert unmanaged pointers to managed references. /// StartGCTracking, /// /// Converts from an object reference (O) to an interior pointer (Ref) pointing to the start of the object. /// /// /// C++/CLI emits "ldarg.1; stloc.0" where arg1 is a string and loc0 is "ref byte" (e.g. as part of the PtrToStringChars codegen); /// we represent this type conversion explicitly in the ILAst. /// ObjectInterior } partial class Conv : UnaryInstruction, ILiftableInstruction { /// /// Gets the conversion kind. /// public readonly ConversionKind Kind; /// /// Gets whether the conversion performs overflow-checking. /// public readonly bool CheckForOverflow; /// /// Gets whether this conversion is a lifted nullable conversion. /// /// /// A lifted conversion expects its argument to be a value of type Nullable{T}, where /// T.GetStackType() == conv.InputType. /// If the value is non-null: /// * it is sign/zero-extended to InputType (based on T's sign) /// * the underlying conversion is performed /// * the result is wrapped in a Nullable{TargetType}. /// If the value is null, the conversion evaluates to default(TargetType?). /// (this result type is underspecified, since there may be multiple C# types for the TargetType) /// public bool IsLifted { get; } /// /// Gets the stack type of the input type. /// /// /// For non-lifted conversions, this is equal to Argument.ResultType. /// For lifted conversions, corresponds to the underlying type of the argument. /// public readonly StackType InputType; /// /// 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; /// /// The target type of the conversion. /// /// /// For lifted conversions, corresponds to the underlying target type. /// /// Target type == PrimitiveType.None can happen for implicit conversions to O in invalid IL. /// public readonly PrimitiveType TargetType; public Conv(ILInstruction argument, PrimitiveType targetType, bool checkForOverflow, Sign inputSign) : this(argument, argument.ResultType, inputSign, targetType, checkForOverflow) { } public Conv(ILInstruction argument, StackType inputType, Sign inputSign, PrimitiveType targetType, bool checkForOverflow, bool isLifted = false) : base(OpCode.Conv, argument) { bool needsSign = checkForOverflow || (!inputType.IsFloatType() && targetType.IsFloatType()); Debug.Assert(!(needsSign && inputSign == Sign.None)); this.InputSign = needsSign ? inputSign : Sign.None; this.InputType = inputType; this.TargetType = targetType; this.CheckForOverflow = checkForOverflow; this.Kind = GetConversionKind(targetType, this.InputType, this.InputSign); // Debug.Assert(Kind != ConversionKind.Invalid); // invalid conversion can happen with invalid IL/missing references this.IsLifted = isLifted; } internal override void CheckInvariant(ILPhase phase) { base.CheckInvariant(phase); // Debug.Assert(Kind != ConversionKind.Invalid); // invalid conversion can happen with invalid IL/missing references Debug.Assert(Argument.ResultType == (IsLifted ? StackType.O : InputType)); Debug.Assert(!(IsLifted && Kind == ConversionKind.StopGCTracking)); } /// /// 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.F4: case StackType.F8: 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.F4: case StackType.F8: 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.F4: case StackType.F8: 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.F4: case StackType.F8: return ConversionKind.FloatToInt; case StackType.Ref: case StackType.O: return ConversionKind.StopGCTracking; default: return ConversionKind.Invalid; } case PrimitiveType.R4: switch (inputType) { case StackType.I4: case StackType.I: case StackType.I8: return ConversionKind.IntToFloat; case StackType.F4: return ConversionKind.Nop; case StackType.F8: return ConversionKind.FloatPrecisionChange; default: return ConversionKind.Invalid; } case PrimitiveType.R: case PrimitiveType.R8: switch (inputType) { case StackType.I4: case StackType.I: case StackType.I8: return ConversionKind.IntToFloat; case StackType.F4: return ConversionKind.FloatPrecisionChange; case StackType.F8: return ConversionKind.Nop; default: return ConversionKind.Invalid; } case PrimitiveType.Ref: // There's no "conv.ref" in IL, but IL allows these conversions implicitly, // whereas we represent them explicitly in the ILAst. switch (inputType) { case StackType.I4: case StackType.I: case StackType.I8: return ConversionKind.StartGCTracking; case StackType.O: return ConversionKind.ObjectInterior; default: return ConversionKind.Invalid; } default: return ConversionKind.Invalid; } } public override StackType ResultType { get => IsLifted ? StackType.O : TargetType.GetStackType(); } public StackType UnderlyingResultType { get => TargetType.GetStackType(); } public override void WriteTo(ITextOutput output, ILAstWritingOptions options) { WriteILRange(output, options); output.Write(OpCode); if (CheckForOverflow) { output.Write(".ovf"); } if (InputSign == Sign.Unsigned) { output.Write(".unsigned"); } else if (InputSign == Sign.Signed) { output.Write(".signed"); } if (IsLifted) { output.Write(".lifted"); } output.Write(' '); output.Write(InputType); 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, options); 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 && !IsLifted) return Argument.UnwrapConv(kind); else return this; } } }