Browse Source

Merge pull request #2856 from ElektroKill/fix/pointer-post-increment

Improve support for post-increment/decrement on pointers
pull/2863/head v8.0-preview3
Daniel Grunwald 3 years ago committed by GitHub
parent
commit
1e009404ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs
  2. 13
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 14
      ICSharpCode.Decompiler/IL/Transforms/TransformAssignment.cs

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs

@ -1,4 +1,4 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy of this // 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 // software and associated documentation files (the "Software"), to deal in the Software
@ -4581,12 +4581,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
return M()[name]++; return M()[name]++;
} }
#if false
public unsafe int PostIncrementOfPointer(int* ptr) public unsafe int PostIncrementOfPointer(int* ptr)
{ {
return *(ptr++); return *(ptr++);
} }
#endif
public int PostDecrementInstanceField() public int PostDecrementInstanceField()
{ {

13
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -1,4 +1,4 @@
// Copyright (c) 2014-2020 Daniel Grunwald // Copyright (c) 2014-2020 Daniel Grunwald
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy of this // 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 // software and associated documentation files (the "Software"), to deal in the Software
@ -1901,7 +1901,16 @@ namespace ICSharpCode.Decompiler.CSharp
if (inst.EvalMode == CompoundEvalMode.EvaluatesToOldValue) if (inst.EvalMode == CompoundEvalMode.EvaluatesToOldValue)
{ {
Debug.Assert(op == AssignmentOperatorType.Add || op == AssignmentOperatorType.Subtract); Debug.Assert(op == AssignmentOperatorType.Add || op == AssignmentOperatorType.Subtract);
Debug.Assert(inst.Value.MatchLdcI(1) || inst.Value.MatchLdcF4(1) || inst.Value.MatchLdcF8(1)); #if DEBUG
if (target.Type is PointerType ptrType)
{
ILInstruction instValue = PointerArithmeticOffset.Detect(inst.Value, ptrType.ElementType, inst.CheckForOverflow);
Debug.Assert(instValue is not null);
Debug.Assert(instValue.MatchLdcI(1));
}
else
Debug.Assert(inst.Value.MatchLdcI(1) || inst.Value.MatchLdcF4(1) || inst.Value.MatchLdcF8(1));
#endif
UnaryOperatorType unary; UnaryOperatorType unary;
ExpressionType exprType; ExpressionType exprType;
if (op == AssignmentOperatorType.Add) if (op == AssignmentOperatorType.Add)

14
ICSharpCode.Decompiler/IL/Transforms/TransformAssignment.cs

@ -1,4 +1,4 @@
// Copyright (c) 2015 Siegfried Pammer // Copyright (c) 2015 Siegfried Pammer
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy of this // 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 // software and associated documentation files (the "Software"), to deal in the Software
@ -871,10 +871,18 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
if (UnwrapSmallIntegerConv(value, out var conv) is BinaryNumericInstruction binary) if (UnwrapSmallIntegerConv(value, out var conv) is BinaryNumericInstruction binary)
{ {
if (!binary.Left.MatchLdLoc(tmpVar) || !(binary.Right.MatchLdcI(1) || binary.Right.MatchLdcF4(1) || binary.Right.MatchLdcF8(1)))
return false;
if (!(binary.Operator == BinaryNumericOperator.Add || binary.Operator == BinaryNumericOperator.Sub)) if (!(binary.Operator == BinaryNumericOperator.Add || binary.Operator == BinaryNumericOperator.Sub))
return false; return false;
if (!binary.Left.MatchLdLoc(tmpVar))
return false;
if (targetType is PointerType ptrType)
{
var right = PointerArithmeticOffset.Detect(binary.Right, ptrType.ElementType, binary.CheckForOverflow);
if (right is null || !right.MatchLdcI(1))
return false;
}
else if (!(binary.Right.MatchLdcI(1) || binary.Right.MatchLdcF4(1) || binary.Right.MatchLdcF8(1)))
return false;
if (!ValidateCompoundAssign(binary, conv, targetType, context.Settings)) if (!ValidateCompoundAssign(binary, conv, targetType, context.Settings))
return false; return false;
context.Step("TransformPostIncDecOperator (builtin)", inst); context.Step("TransformPostIncDecOperator (builtin)", inst);

Loading…
Cancel
Save