[zig]: fix use of dead stack variables #31
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Howdy! Thanks for making this nifty project. I wanted to use it to take a data point to find out machine code quality of Zig's old compiler vs the new compiler. So I tried compiling it without
-fstage1
but ran into ziglang/zig#13340. I worked around that with a small diff toSprite.Flags
inside this PR which perhaps you will find acceptable (alternately you could just wait until that issue is fixed and then that part of this PR could be dropped).Next, I hit a segfault, and so I ran the binary in Valgrind, and there were a lot of errors like this:
So that lead to reworking
GameBoy.new
intoGameBoy.init
to avoid keeping pointers to dead stack variables. It's too bad that code like what you had originally doesn't work; that pattern is the motivation behind this proposal: ziglang/zig#2765Who knows, maybe we'll get that pattern working someday. But as it stands, that code is not legal because dead stack variables are being accessed. Related: ziglang/zig#3180 - this will make debugging such issues more deterministic.
After fixing that issue, Valgrind got much quieter, only showing two things left:
So I peeked into the SDL sdk.zig file and noticed that
SDL.Renderer
is a struct that wraps a pointer, so no need to take a double pointer. This prevents hanging onto a pointer to the wrapping struct when what is really desired is a copy of the pointer.After fixing that, Valgrind went silent and the binary started working. Then I was able to collect the data point that I wanted:
Sweet! Looks like the new compiler generates better LLVM IR than the old one 🙂