Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer Arithmetic Prettiness #3411

Open
ds5678 opened this issue Feb 25, 2025 · 0 comments · May be fixed by #3413
Open

Pointer Arithmetic Prettiness #3411

ds5678 opened this issue Feb 25, 2025 · 0 comments · May be fixed by #3413
Labels
Enhancement Areas for improvement

Comments

@ds5678
Copy link
Contributor

ds5678 commented Feb 25, 2025

Is your feature request related to a problem? Please describe.

I'm decompiling code that uses lots of pointer casts. Although the decompiled expressions using byte* are functionally correct, they're not very pretty.

Source:

public static class C
{
    public unsafe static void Assignment(void* ptr)
    {
        ((int*)ptr)[2] = 1;
    }

    public unsafe static int Access(void* ptr)
    {
        return ((int*)ptr)[2];
    }

    public unsafe static void Assignment(long* ptr)
    {
        ((int*)ptr)[2] = 1;
    }

    public unsafe static int Access(long* ptr)
    {
        return ((int*)ptr)[2];
    }

    public unsafe static void Assignment(int* ptr)
    {
        ptr[2] = 1;
    }

    public unsafe static int Access(int* ptr)
    {
        return ptr[2];
    }
}

Decompiled:

public static class C
{
    public unsafe static void Assignment(void* ptr)
    {
        *(int*)((byte*)ptr + (nint)2 * (nint)4) = 1;
    }

    public unsafe static int Access(void* ptr)
    {
        return *(int*)((byte*)ptr + (nint)2 * (nint)4);
    }

    public unsafe static void Assignment(long* ptr)
    {
        *(int*)((byte*)ptr + (nint)2 * (nint)4) = 1;
    }

    public unsafe static int Access(long* ptr)
    {
        return *(int*)((byte*)ptr + (nint)2 * (nint)4);
    }

    public unsafe static void Assignment(int* ptr)
    {
        ptr[2] = 1;
    }

    public unsafe static int Access(int* ptr)
    {
        return ptr[2];
    }
}

https://sharplab.io/#v2:CYLg1APgAgzABFAjANgQJjgYQLACgDeecxC8ArgHYDOAhgGYCmCKCALHAIJVUCWA5hQC2DCgBcAFFFYAqOAAdRAJwCUREoVwktcceJ5jpyhSoDaaALpwAvHEQBuNcQC+eR6TiVajZqn2jOAMYBDNySMvJKqprqblpQAOw6egZGkWbmDtHOrlnunvRMSKhSnNz8QiISADYA9hR8ssZRWhraJLp+hk3p1raZWi64brAe1AU+cH6BwaG19Y2Rbq1tCIkdKd0W/SSDw+Rj3kVspbwCwmLJogsqS7Ekxj029m67uSP5hyxTHEEhVJfXZoxXJxRIPLYvPBOIA=

Describe the solution you'd like

Ideally, ILSpy could detect this pattern and decompile the code in a more pretty way.

Additional context

I'm open to working on this. I think it can probably be fixed somewhere around here:

TranslatedExpression offsetExpr = GetPointerArithmeticOffset(byteOffsetInst, byteOffsetExpr, pointerType.ElementType, inst.CheckForOverflow)
?? FallBackToBytePointer();

@ds5678 ds5678 added the Enhancement Areas for improvement label Feb 25, 2025
@ds5678 ds5678 linked a pull request Feb 25, 2025 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Areas for improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant