-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Use type hint in pointer arithmetic #3413
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
Use type hint in pointer arithmetic #3413
Conversation
|
Could we have an option for this? I think it's a matter of preference which version of the decompilation is preferred (using only byte offsets or the offsets using the size of the type of the pointer), and it could be something that is configurable. |
|
An option for what specifically?
|
Your change makes the decompiler change the pointer arithmetic to be based on the size of the pointer type, while previously, it used to use byte offsets and then cast it to the necessary type pointer. I suggest an option to make this new behavior a toggleable option since, in my opinion, it's really a matter of preference. TLDR: Make the change in PR toggleable with a deecompiler option. |
|
I don't see why this: *(int*)((byte*)ptr + (nint)2 * (nint)4) = 1;would be preferable to: ((int*)ptr)[2] = 1; |
Yes, this is clearer. However, this is not what initially sparked my suggestion for the option to be added. What made me suggest the option was the changes that occurred in this test as a effect of the change: The lines: *(int*)(num - 4) = 2;
*(int*)(num - 8) = 3;got changed to: *((int*)num - 1) = 2;
*((int*)num - 2) = 3;In my opinion, the code decompilation before the change was clearer as we are working on a |
|
While there is room for tweaking the rules about when to use the type hint instead of the natural type, I disagree that it carries enough importance to be elevated to a configuration option. I have a few suggestions for a rule change:
What are your thoughts? |
I think tweaking the output of pointer arithmetic is indeed the best option. In my opinion something like this would be good:
Let me know what you think! |
It sounds like you prefer this modification to the current rules. The rest of your bullet points are already part of the current rule set. |
|
Actually, I'm not sure that's the best change. It results in your desired outcome. // Source and decompiled the same
*(int*)(ptr - 8) = 3;However, it also makes this code decompile poorly. // Source
*((int*)ptr - 2) = 3;
// Decompiled
*(int*)(ptr - (nint)2 * (nint)4) = 3; |
|
Oh, I'd expect the decompiler here to simplify the multiplication there, considering it already does simplify some simple arithmetic when converting the ILAst to C#. I think this is because of the *(int*)(ptr - (nint)2 * (nint)4) = 3;*(int*)(ptr - 8) = 3;There really is no way to decompile these kinds of expressions the same way they are written, and we just need to find the best compromise, so I'm not sure what the best way is to approach this. |
|
@ds5678 I don't want to comment on the changes in this PR, because it's @dgrunwald's area of expertise. Thank you for your understanding... |
91050e0 to
4c4a02d
Compare
0055cb5 to
8de6585
Compare
I notice that he doesn't seem to be active on GitHub. To set proper expectations, how long should we wait for his review before moving forward? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change looks good except for the small detail with the repeated call.
|
Thank you for the review! I implemented the change you requested. |
Resolves #3411
Problem
This makes pointer arithmetic prettier with the help of available type hints.
Solution
sbyte*vsbyte*. I want to know if that's okay or not. Previously,byte*got picked because it's the default when things go wrong. However, now the type hint (which is often signed) prevents some of those situations.