Browse Source

Fix pointer arithmetic with `nint*`/`nuint*`.

pull/2063/head
Daniel Grunwald 5 years ago
parent
commit
59be020dea
  1. 10
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/NativeInts.cs
  2. 5
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 2
      ICSharpCode.Decompiler/IL/PointerArithmeticOffset.cs

10
ICSharpCode.Decompiler.Tests/TestCases/Pretty/NativeInts.cs

@ -109,6 +109,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -109,6 +109,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine(-i);
}
public unsafe int* PtrArithmetic(int* ptr)
{
return ptr + i;
}
public unsafe nint* PtrArithmetic(nint* ptr)
{
return ptr + u;
}
public object[] Boxing()
{
return new object[10] {

5
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -1143,10 +1143,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1143,10 +1143,7 @@ namespace ICSharpCode.Decompiler.CSharp
{
if (!expr.Type.IsCSharpPrimitiveIntegerType() && !expr.Type.IsCSharpNativeIntegerType()) {
// pointer arithmetic accepts all primitive integer types, but no enums etc.
StackType targetType = expr.Type.GetStackType() == StackType.I4 ? StackType.I4 : StackType.I8;
expr = expr.ConvertTo(
compilation.FindType(targetType.ToKnownTypeCode(expr.Type.GetSign())),
this);
expr = expr.ConvertTo(FindArithmeticType(expr.Type.GetStackType(), expr.Type.GetSign()), this);
}
return expr;
}

2
ICSharpCode.Decompiler/IL/PointerArithmeticOffset.cs

@ -36,7 +36,7 @@ namespace ICSharpCode.Decompiler.IL @@ -36,7 +36,7 @@ namespace ICSharpCode.Decompiler.IL
if (mul.CheckForOverflow != checkForOverflow)
return null;
if (elementSize > 0 && mul.Right.MatchLdcI(elementSize.Value)
|| mul.Right.UnwrapConv(ConversionKind.SignExtend) is SizeOf sizeOf && sizeOf.Type.Equals(pointerElementType)) {
|| mul.Right.UnwrapConv(ConversionKind.SignExtend) is SizeOf sizeOf && NormalizeTypeVisitor.TypeErasure.EquivalentTypes(sizeOf.Type, pointerElementType)) {
var countOffsetInst = mul.Left;
if (unwrapZeroExtension) {
countOffsetInst = countOffsetInst.UnwrapConv(ConversionKind.ZeroExtend);

Loading…
Cancel
Save