Browse Source

Fix pointer arithmetic involving pointer to structs and long offsets.

pull/924/head
Daniel Grunwald 8 years ago
parent
commit
bab93ee990
  1. 49
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs
  2. 210
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.il
  3. 156
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.opt.il
  4. 152
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.opt.roslyn.il
  5. 206
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.roslyn.il
  6. 7
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

49
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs

@ -20,7 +20,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{ {
public class UnsafeCode public class UnsafeCode
{ {
private struct SimpleStruct public struct SimpleStruct
{ {
public int X; public int X;
public double Y; public double Y;
@ -150,7 +150,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
return p + 2; return p + 2;
} }
public unsafe long* PointerArithmetic2(long* p, int y, int x) public unsafe long* PointerArithmetic2(long* p)
{ {
return 3 + p; return 3 + p;
} }
@ -170,6 +170,41 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
return q[i] + *(byte*)p; return q[i] + *(byte*)p;
} }
public unsafe int PointerArithmetic6(SimpleStruct* p, int i)
{
return p[i].X;
}
public unsafe int* PointerArithmeticLong1(int* p, long offset)
{
return p + offset;
}
public unsafe int* PointerArithmeticLong2(int* p, long offset)
{
return offset + p;
}
public unsafe int* PointerArithmeticLong3(int* p, long offset)
{
return p - offset;
}
public unsafe SimpleStruct* PointerArithmeticLong1s(SimpleStruct* p, long offset)
{
return p + offset;
}
public unsafe SimpleStruct* PointerArithmeticLong2s(SimpleStruct* p, long offset)
{
return offset + p;
}
public unsafe SimpleStruct* PointerArithmeticLong3s(SimpleStruct* p, long offset)
{
return p - offset;
}
public unsafe int PointerSubtraction(long* p, long* q) public unsafe int PointerSubtraction(long* p, long* q)
{ {
return (int)(p - q); return (int)(p - q);
@ -190,6 +225,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
return (int)((byte*)p - (byte*)q); return (int)((byte*)p - (byte*)q);
} }
public unsafe long PointerSubtraction4(sbyte* p, sbyte* q)
{
return p - q;
}
public unsafe long PointerSubtraction5(SimpleStruct* p, SimpleStruct* q)
{
return p - q;
}
unsafe ~UnsafeCode() unsafe ~UnsafeCode()
{ {
this.PassPointerAsRefParameter(this.NullPointer); this.PassPointerAsRefParameter(this.NullPointer);

210
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.il

@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0 .ver 4:0:0:0
} }
.assembly '40okgik0' .assembly p1cal2uj
{ {
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
@ -20,15 +20,15 @@
.hash algorithm 0x00008004 .hash algorithm 0x00008004
.ver 0:0:0:0 .ver 0:0:0:0
} }
.module '40okgik0.dll' .module p1cal2uj.dll
// MVID: {668B0983-7797-493E-81D1-A040A24E86AA} // MVID: {BD577496-1116-4E18-AF25-DE99056CECA8}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000 .imagebase 0x10000000
.file alignment 0x00000200 .file alignment 0x00000200
.stackreserve 0x00100000 .stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI .subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY .corflags 0x00000001 // ILONLY
// Image base: 0x00C20000 // Image base: 0x03220000
// =============== CLASS MEMBERS DECLARATION =================== // =============== CLASS MEMBERS DECLARATION ===================
@ -36,7 +36,7 @@
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode .class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
extends [mscorlib]System.Object extends [mscorlib]System.Object
{ {
.class sequential ansi sealed nested private beforefieldinit SimpleStruct .class sequential ansi sealed nested public beforefieldinit SimpleStruct
extends [mscorlib]System.ValueType extends [mscorlib]System.ValueType
{ {
.field public int32 X .field public int32 X
@ -501,9 +501,7 @@
} // end of method UnsafeCode::PointerArithmetic } // end of method UnsafeCode::PointerArithmetic
.method public hidebysig instance int64* .method public hidebysig instance int64*
PointerArithmetic2(int64* p, PointerArithmetic2(int64* p) cil managed
int32 y,
int32 x) cil managed
{ {
// Code size 11 (0xb) // Code size 11 (0xb)
.maxstack 2 .maxstack 2
@ -579,6 +577,160 @@
IL_000c: ret IL_000c: ret
} // end of method UnsafeCode::PointerArithmetic5 } // end of method UnsafeCode::PointerArithmetic5
.method public hidebysig instance int32
PointerArithmetic6(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int32 i) cil managed
{
// Code size 22 (0x16)
.maxstack 3
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: conv.i
IL_0004: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_000a: mul
IL_000b: add
IL_000c: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct::X
IL_0011: stloc.0
IL_0012: br.s IL_0014
IL_0014: ldloc.0
IL_0015: ret
} // end of method UnsafeCode::PointerArithmetic6
.method public hidebysig instance int32*
PointerArithmeticLong1(int32* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 3
.locals init (int32* V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: ldc.i4.4
IL_0004: conv.i8
IL_0005: mul
IL_0006: conv.i
IL_0007: add
IL_0008: stloc.0
IL_0009: br.s IL_000b
IL_000b: ldloc.0
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong1
.method public hidebysig instance int32*
PointerArithmeticLong2(int32* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 2
.locals init (int32* V_0)
IL_0000: nop
IL_0001: ldarg.2
IL_0002: ldc.i4.4
IL_0003: conv.i8
IL_0004: mul
IL_0005: conv.i
IL_0006: ldarg.1
IL_0007: add
IL_0008: stloc.0
IL_0009: br.s IL_000b
IL_000b: ldloc.0
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong2
.method public hidebysig instance int32*
PointerArithmeticLong3(int32* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 3
.locals init (int32* V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: ldc.i4.4
IL_0004: conv.i8
IL_0005: mul
IL_0006: conv.i
IL_0007: sub
IL_0008: stloc.0
IL_0009: br.s IL_000b
IL_000b: ldloc.0
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong3
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong1s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 18 (0x12)
.maxstack 3
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0009: conv.i8
IL_000a: mul
IL_000b: conv.i
IL_000c: add
IL_000d: stloc.0
IL_000e: br.s IL_0010
IL_0010: ldloc.0
IL_0011: ret
} // end of method UnsafeCode::PointerArithmeticLong1s
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong2s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 18 (0x12)
.maxstack 2
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* V_0)
IL_0000: nop
IL_0001: ldarg.2
IL_0002: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0008: conv.i8
IL_0009: mul
IL_000a: conv.i
IL_000b: ldarg.1
IL_000c: add
IL_000d: stloc.0
IL_000e: br.s IL_0010
IL_0010: ldloc.0
IL_0011: ret
} // end of method UnsafeCode::PointerArithmeticLong2s
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong3s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 18 (0x12)
.maxstack 3
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0009: conv.i8
IL_000a: mul
IL_000b: conv.i
IL_000c: sub
IL_000d: stloc.0
IL_000e: br.s IL_0010
IL_0010: ldloc.0
IL_0011: ret
} // end of method UnsafeCode::PointerArithmeticLong3s
.method public hidebysig instance int32 .method public hidebysig instance int32
PointerSubtraction(int64* p, PointerSubtraction(int64* p,
int64* q) cil managed int64* q) cil managed
@ -666,6 +818,48 @@
IL_000c: ret IL_000c: ret
} // end of method UnsafeCode::PointerSubtraction3 } // end of method UnsafeCode::PointerSubtraction3
.method public hidebysig instance int64
PointerSubtraction4(int8* p,
int8* q) cil managed
{
// Code size 12 (0xc)
.maxstack 2
.locals init (int64 V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: sub
IL_0004: ldc.i4.1
IL_0005: div
IL_0006: conv.i8
IL_0007: stloc.0
IL_0008: br.s IL_000a
IL_000a: ldloc.0
IL_000b: ret
} // end of method UnsafeCode::PointerSubtraction4
.method public hidebysig instance int64
PointerSubtraction5(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* q) cil managed
{
// Code size 17 (0x11)
.maxstack 2
.locals init (int64 V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: sub
IL_0004: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_000a: div
IL_000b: conv.i8
IL_000c: stloc.0
IL_000d: br.s IL_000f
IL_000f: ldloc.0
IL_0010: ret
} // end of method UnsafeCode::PointerSubtraction5
.method family hidebysig virtual instance void .method family hidebysig virtual instance void
Finalize() cil managed Finalize() cil managed
{ {

156
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.opt.il

@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0 .ver 4:0:0:0
} }
.assembly '0iyybovr' .assembly '0mxa1uxq'
{ {
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
@ -20,15 +20,15 @@
.hash algorithm 0x00008004 .hash algorithm 0x00008004
.ver 0:0:0:0 .ver 0:0:0:0
} }
.module '0iyybovr.dll' .module '0mxa1uxq.dll'
// MVID: {4E5B1C69-64A7-4276-A009-A657A9B697D4} // MVID: {EDB443DE-1443-48F8-8E42-03BAFC654447}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000 .imagebase 0x10000000
.file alignment 0x00000200 .file alignment 0x00000200
.stackreserve 0x00100000 .stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI .subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY .corflags 0x00000001 // ILONLY
// Image base: 0x02F80000 // Image base: 0x00B60000
// =============== CLASS MEMBERS DECLARATION =================== // =============== CLASS MEMBERS DECLARATION ===================
@ -36,7 +36,7 @@
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode .class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
extends [mscorlib]System.Object extends [mscorlib]System.Object
{ {
.class sequential ansi sealed nested private beforefieldinit SimpleStruct .class sequential ansi sealed nested public beforefieldinit SimpleStruct
extends [mscorlib]System.ValueType extends [mscorlib]System.ValueType
{ {
.field public int32 X .field public int32 X
@ -400,9 +400,7 @@
} // end of method UnsafeCode::PointerArithmetic } // end of method UnsafeCode::PointerArithmetic
.method public hidebysig instance int64* .method public hidebysig instance int64*
PointerArithmetic2(int64* p, PointerArithmetic2(int64* p) cil managed
int32 y,
int32 x) cil managed
{ {
// Code size 6 (0x6) // Code size 6 (0x6)
.maxstack 8 .maxstack 8
@ -454,6 +452,118 @@
IL_0007: ret IL_0007: ret
} // end of method UnsafeCode::PointerArithmetic5 } // end of method UnsafeCode::PointerArithmetic5
.method public hidebysig instance int32
PointerArithmetic6(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int32 i) cil managed
{
// Code size 17 (0x11)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: conv.i
IL_0003: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0009: mul
IL_000a: add
IL_000b: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct::X
IL_0010: ret
} // end of method UnsafeCode::PointerArithmetic6
.method public hidebysig instance int32*
PointerArithmeticLong1(int32* p,
int64 offset) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: ldc.i4.4
IL_0003: conv.i8
IL_0004: mul
IL_0005: conv.i
IL_0006: add
IL_0007: ret
} // end of method UnsafeCode::PointerArithmeticLong1
.method public hidebysig instance int32*
PointerArithmeticLong2(int32* p,
int64 offset) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.2
IL_0001: ldc.i4.4
IL_0002: conv.i8
IL_0003: mul
IL_0004: conv.i
IL_0005: ldarg.1
IL_0006: add
IL_0007: ret
} // end of method UnsafeCode::PointerArithmeticLong2
.method public hidebysig instance int32*
PointerArithmeticLong3(int32* p,
int64 offset) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: ldc.i4.4
IL_0003: conv.i8
IL_0004: mul
IL_0005: conv.i
IL_0006: sub
IL_0007: ret
} // end of method UnsafeCode::PointerArithmeticLong3
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong1s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0008: conv.i8
IL_0009: mul
IL_000a: conv.i
IL_000b: add
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong1s
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong2s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldarg.2
IL_0001: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0007: conv.i8
IL_0008: mul
IL_0009: conv.i
IL_000a: ldarg.1
IL_000b: add
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong2s
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong3s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0008: conv.i8
IL_0009: mul
IL_000a: conv.i
IL_000b: sub
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong3s
.method public hidebysig instance int32 .method public hidebysig instance int32
PointerSubtraction(int64* p, PointerSubtraction(int64* p,
int64* q) cil managed int64* q) cil managed
@ -517,6 +627,36 @@
IL_0007: ret IL_0007: ret
} // end of method UnsafeCode::PointerSubtraction3 } // end of method UnsafeCode::PointerSubtraction3
.method public hidebysig instance int64
PointerSubtraction4(int8* p,
int8* q) cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: sub
IL_0003: ldc.i4.1
IL_0004: div
IL_0005: conv.i8
IL_0006: ret
} // end of method UnsafeCode::PointerSubtraction4
.method public hidebysig instance int64
PointerSubtraction5(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* q) cil managed
{
// Code size 12 (0xc)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: sub
IL_0003: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0009: div
IL_000a: conv.i8
IL_000b: ret
} // end of method UnsafeCode::PointerSubtraction5
.method family hidebysig virtual instance void .method family hidebysig virtual instance void
Finalize() cil managed Finalize() cil managed
{ {

152
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.opt.roslyn.il

@ -25,14 +25,14 @@
.ver 0:0:0:0 .ver 0:0:0:0
} }
.module UnsafeCode.dll .module UnsafeCode.dll
// MVID: {98803778-6CDE-4C69-BDF2-64568153FD06} // MVID: {921693A0-E269-428D-8AB1-E3B34745B42B}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000 .imagebase 0x10000000
.file alignment 0x00000200 .file alignment 0x00000200
.stackreserve 0x00100000 .stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI .subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY .corflags 0x00000001 // ILONLY
// Image base: 0x02790000 // Image base: 0x033D0000
// =============== CLASS MEMBERS DECLARATION =================== // =============== CLASS MEMBERS DECLARATION ===================
@ -40,7 +40,7 @@
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode .class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
extends [mscorlib]System.Object extends [mscorlib]System.Object
{ {
.class sequential ansi sealed nested private beforefieldinit SimpleStruct .class sequential ansi sealed nested public beforefieldinit SimpleStruct
extends [mscorlib]System.ValueType extends [mscorlib]System.ValueType
{ {
.field public int32 X .field public int32 X
@ -406,9 +406,7 @@
} // end of method UnsafeCode::PointerArithmetic } // end of method UnsafeCode::PointerArithmetic
.method public hidebysig instance int64* .method public hidebysig instance int64*
PointerArithmetic2(int64* p, PointerArithmetic2(int64* p) cil managed
int32 y,
int32 x) cil managed
{ {
// Code size 7 (0x7) // Code size 7 (0x7)
.maxstack 8 .maxstack 8
@ -460,6 +458,118 @@
IL_0007: ret IL_0007: ret
} // end of method UnsafeCode::PointerArithmetic5 } // end of method UnsafeCode::PointerArithmetic5
.method public hidebysig instance int32
PointerArithmetic6(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int32 i) cil managed
{
// Code size 17 (0x11)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: conv.i
IL_0003: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0009: mul
IL_000a: add
IL_000b: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct::X
IL_0010: ret
} // end of method UnsafeCode::PointerArithmetic6
.method public hidebysig instance int32*
PointerArithmeticLong1(int32* p,
int64 offset) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: ldc.i4.4
IL_0003: conv.i8
IL_0004: mul
IL_0005: conv.i
IL_0006: add
IL_0007: ret
} // end of method UnsafeCode::PointerArithmeticLong1
.method public hidebysig instance int32*
PointerArithmeticLong2(int32* p,
int64 offset) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.2
IL_0001: ldc.i4.4
IL_0002: conv.i8
IL_0003: mul
IL_0004: conv.i
IL_0005: ldarg.1
IL_0006: add
IL_0007: ret
} // end of method UnsafeCode::PointerArithmeticLong2
.method public hidebysig instance int32*
PointerArithmeticLong3(int32* p,
int64 offset) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: ldc.i4.4
IL_0003: conv.i8
IL_0004: mul
IL_0005: conv.i
IL_0006: sub
IL_0007: ret
} // end of method UnsafeCode::PointerArithmeticLong3
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong1s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0008: conv.i8
IL_0009: mul
IL_000a: conv.i
IL_000b: add
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong1s
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong2s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldarg.2
IL_0001: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0007: conv.i8
IL_0008: mul
IL_0009: conv.i
IL_000a: ldarg.1
IL_000b: add
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong2s
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong3s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0008: conv.i8
IL_0009: mul
IL_000a: conv.i
IL_000b: sub
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong3s
.method public hidebysig instance int32 .method public hidebysig instance int32
PointerSubtraction(int64* p, PointerSubtraction(int64* p,
int64* q) cil managed int64* q) cil managed
@ -523,6 +633,36 @@
IL_0007: ret IL_0007: ret
} // end of method UnsafeCode::PointerSubtraction3 } // end of method UnsafeCode::PointerSubtraction3
.method public hidebysig instance int64
PointerSubtraction4(int8* p,
int8* q) cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: sub
IL_0003: ldc.i4.1
IL_0004: div
IL_0005: conv.i8
IL_0006: ret
} // end of method UnsafeCode::PointerSubtraction4
.method public hidebysig instance int64
PointerSubtraction5(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* q) cil managed
{
// Code size 12 (0xc)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: sub
IL_0003: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0009: div
IL_000a: conv.i8
IL_000b: ret
} // end of method UnsafeCode::PointerSubtraction5
.method family hidebysig virtual instance void .method family hidebysig virtual instance void
Finalize() cil managed Finalize() cil managed
{ {

206
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.roslyn.il

@ -25,14 +25,14 @@
.ver 0:0:0:0 .ver 0:0:0:0
} }
.module UnsafeCode.dll .module UnsafeCode.dll
// MVID: {8E29CB23-0809-4825-9EB4-4274E16E25C8} // MVID: {7BB1F8C9-F0A0-4D87-9A69-19FAC9915AFA}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000 .imagebase 0x10000000
.file alignment 0x00000200 .file alignment 0x00000200
.stackreserve 0x00100000 .stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI .subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY .corflags 0x00000001 // ILONLY
// Image base: 0x00690000 // Image base: 0x015B0000
// =============== CLASS MEMBERS DECLARATION =================== // =============== CLASS MEMBERS DECLARATION ===================
@ -40,7 +40,7 @@
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode .class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
extends [mscorlib]System.Object extends [mscorlib]System.Object
{ {
.class sequential ansi sealed nested private beforefieldinit SimpleStruct .class sequential ansi sealed nested public beforefieldinit SimpleStruct
extends [mscorlib]System.ValueType extends [mscorlib]System.ValueType
{ {
.field public int32 X .field public int32 X
@ -507,9 +507,7 @@
} // end of method UnsafeCode::PointerArithmetic } // end of method UnsafeCode::PointerArithmetic
.method public hidebysig instance int64* .method public hidebysig instance int64*
PointerArithmetic2(int64* p, PointerArithmetic2(int64* p) cil managed
int32 y,
int32 x) cil managed
{ {
// Code size 12 (0xc) // Code size 12 (0xc)
.maxstack 2 .maxstack 2
@ -585,6 +583,160 @@
IL_000c: ret IL_000c: ret
} // end of method UnsafeCode::PointerArithmetic5 } // end of method UnsafeCode::PointerArithmetic5
.method public hidebysig instance int32
PointerArithmetic6(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int32 i) cil managed
{
// Code size 22 (0x16)
.maxstack 3
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: conv.i
IL_0004: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_000a: mul
IL_000b: add
IL_000c: ldfld int32 ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct::X
IL_0011: stloc.0
IL_0012: br.s IL_0014
IL_0014: ldloc.0
IL_0015: ret
} // end of method UnsafeCode::PointerArithmetic6
.method public hidebysig instance int32*
PointerArithmeticLong1(int32* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 3
.locals init (int32* V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: ldc.i4.4
IL_0004: conv.i8
IL_0005: mul
IL_0006: conv.i
IL_0007: add
IL_0008: stloc.0
IL_0009: br.s IL_000b
IL_000b: ldloc.0
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong1
.method public hidebysig instance int32*
PointerArithmeticLong2(int32* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 2
.locals init (int32* V_0)
IL_0000: nop
IL_0001: ldarg.2
IL_0002: ldc.i4.4
IL_0003: conv.i8
IL_0004: mul
IL_0005: conv.i
IL_0006: ldarg.1
IL_0007: add
IL_0008: stloc.0
IL_0009: br.s IL_000b
IL_000b: ldloc.0
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong2
.method public hidebysig instance int32*
PointerArithmeticLong3(int32* p,
int64 offset) cil managed
{
// Code size 13 (0xd)
.maxstack 3
.locals init (int32* V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: ldc.i4.4
IL_0004: conv.i8
IL_0005: mul
IL_0006: conv.i
IL_0007: sub
IL_0008: stloc.0
IL_0009: br.s IL_000b
IL_000b: ldloc.0
IL_000c: ret
} // end of method UnsafeCode::PointerArithmeticLong3
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong1s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 18 (0x12)
.maxstack 3
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0009: conv.i8
IL_000a: mul
IL_000b: conv.i
IL_000c: add
IL_000d: stloc.0
IL_000e: br.s IL_0010
IL_0010: ldloc.0
IL_0011: ret
} // end of method UnsafeCode::PointerArithmeticLong1s
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong2s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 18 (0x12)
.maxstack 2
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* V_0)
IL_0000: nop
IL_0001: ldarg.2
IL_0002: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0008: conv.i8
IL_0009: mul
IL_000a: conv.i
IL_000b: ldarg.1
IL_000c: add
IL_000d: stloc.0
IL_000e: br.s IL_0010
IL_0010: ldloc.0
IL_0011: ret
} // end of method UnsafeCode::PointerArithmeticLong2s
.method public hidebysig instance valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct*
PointerArithmeticLong3s(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
int64 offset) cil managed
{
// Code size 18 (0x12)
.maxstack 3
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_0009: conv.i8
IL_000a: mul
IL_000b: conv.i
IL_000c: sub
IL_000d: stloc.0
IL_000e: br.s IL_0010
IL_0010: ldloc.0
IL_0011: ret
} // end of method UnsafeCode::PointerArithmeticLong3s
.method public hidebysig instance int32 .method public hidebysig instance int32
PointerSubtraction(int64* p, PointerSubtraction(int64* p,
int64* q) cil managed int64* q) cil managed
@ -672,6 +824,48 @@
IL_000c: ret IL_000c: ret
} // end of method UnsafeCode::PointerSubtraction3 } // end of method UnsafeCode::PointerSubtraction3
.method public hidebysig instance int64
PointerSubtraction4(int8* p,
int8* q) cil managed
{
// Code size 12 (0xc)
.maxstack 2
.locals init (int64 V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: sub
IL_0004: ldc.i4.1
IL_0005: div
IL_0006: conv.i8
IL_0007: stloc.0
IL_0008: br.s IL_000a
IL_000a: ldloc.0
IL_000b: ret
} // end of method UnsafeCode::PointerSubtraction4
.method public hidebysig instance int64
PointerSubtraction5(valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* p,
valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct* q) cil managed
{
// Code size 17 (0x11)
.maxstack 2
.locals init (int64 V_0)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.2
IL_0003: sub
IL_0004: sizeof ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/SimpleStruct
IL_000a: div
IL_000b: conv.i8
IL_000c: stloc.0
IL_000d: br.s IL_000f
IL_000f: ldloc.0
IL_0010: ret
} // end of method UnsafeCode::PointerSubtraction5
.method family hidebysig virtual instance void .method family hidebysig virtual instance void
Finalize() cil managed Finalize() cil managed
{ {

7
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -759,6 +759,9 @@ namespace ICSharpCode.Decompiler.CSharp
TranslatedExpression? GetPointerArithmeticOffset() TranslatedExpression? GetPointerArithmeticOffset()
{ {
if (byteOffsetInst is Conv conv && conv.InputType == StackType.I8 && conv.ResultType == StackType.I) {
byteOffsetInst = conv.Argument;
}
int? elementSize = ComputeSizeOf(pointerType.ElementType); int? elementSize = ComputeSizeOf(pointerType.ElementType);
if (elementSize == 1) { if (elementSize == 1) {
return byteOffsetExpr; return byteOffsetExpr;
@ -769,6 +772,8 @@ namespace ICSharpCode.Decompiler.CSharp
return null; return null;
if (elementSize > 0 && mul.Right.MatchLdcI(elementSize.Value)) { if (elementSize > 0 && mul.Right.MatchLdcI(elementSize.Value)) {
return Translate(mul.Left); return Translate(mul.Left);
} else if (mul.Right.UnwrapConv(ConversionKind.SignExtend) is SizeOf sizeOf && sizeOf.Type.Equals(pointerType.ElementType)) {
return Translate(mul.Left);
} }
} else if (byteOffsetInst.MatchLdcI(out long val)) { } else if (byteOffsetInst.MatchLdcI(out long val)) {
// If the offset is a constant, it's possible that the compiler // If the offset is a constant, it's possible that the compiler
@ -812,7 +817,7 @@ namespace ICSharpCode.Decompiler.CSharp
if (inst.Right.MatchLdcI(out long elementSize)) { if (inst.Right.MatchLdcI(out long elementSize)) {
elementType = null; elementType = null;
// OK, might be pointer subtraction if the element size matches // OK, might be pointer subtraction if the element size matches
} else if (inst.Right.MatchSizeOf(out elementType)) { } else if (inst.Right.UnwrapConv(ConversionKind.SignExtend).MatchSizeOf(out elementType)) {
// OK, might be pointer subtraction if the element type matches // OK, might be pointer subtraction if the element type matches
} else { } else {
return null; return null;

Loading…
Cancel
Save