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

[optimizer] Fix in-block jump offsets resolved wrong inside blocks with NOP statements. #1042

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

YanayGoor
Copy link

I was playing around with implementing stuff in gencode.c when I encountered two bugs that can occur when you have a block with in-block jumps (statements with jt and jf pointing to other statements in the block) that also has NOPs.

The problem stems from the offset array, the size of the array is calculated by the function slength which does not count NOPs even though the array is populated with NOPs.
This causes two things:

  • the offset array does not contains all of the statements in the block, and thus trying to jump to one of the missing statements at the end of the block fails unless -O is given.
  • even if the statement can be found, if there are any NOPs before the target statement the offset will include them even though they are about to be removed, therefore the final jump will be too far ahead and extra statements will be skipped.

The naïve solution would be to not include the NOPs in the offset table, and even though that will solve these two problems, it will cause a different bug where you cannot jump to a statement that is being optimized out.
Assuming all optimizations take n statement and replace first n-1 with NOPs, if we jump to a NOP I think the wanted behavior is to jump to the nth statement.

So, since the offset array is generated "for convenience", and since we can see it both causes bugs and takes up more lines of code then the trivial approach, I think that removing it is the right fix for these problems.

I have created two minimal examples of the bugs in this branch
without fix:
image

with fix:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant