CBE: add C type infrastructure#14691
Merged
andrewrk merged 12 commits intoziglang:masterfrom Feb 22, 2023
Merged
Conversation
This requires manual defines before C99 which may not have stdint.h. Also have update-zig1 leave a copy of lib/zig.h in stage1/zig.h, which allows lib/zig.h to be updated without needing to update zig1.wasm. Note that since the object already existed with the exact same contents, this completely avoids repo bloat due to zig.h changes.
Adds a new mechanism for `@tagName` function generation that doesn't piggyback on the removed typedef system.
By factoring out the comptime parts of this computation, vectors are no longer useful in this function.
kcbanner
reviewed
Feb 21, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The main motivation for this PR was that in the C backend, the key of the typedef hash maps used to be a (Zig)
Typewhich did not very accurately model C types. Also, the typedef sorting code started to depend on hacks where certainTypes were being used for a non-obvious purposes, like Zig pointers "modeling" C forward declarations (???). This introduces aCTypestruct which can be used as a key instead and models the C language more closely (although not perfectly, in ways specifically tailored to the current needs of the C backend). UnlikeTypes (as of the time of writing)CTypes are interned within each hash map and have stable handle-like indices once created. This both reduces memory usage that would normally be needed to store the duplicated state and improves things like comparisons betweenCTypes and managing the difficulty of merging certain similar types so that they can be used interchangeably in the generated C code. Most generated typedefs have been removed for no particular reason, however I found a way to use typedefs for anonymous aggregate types that doesn't require relying on a hash to produce unique type names. This does introduce a bit of complexity into the final module flush that used to be duplicated among many different declaration generation passes (of dubious benefit), however now that it is just iterating over C types and printing C types without needing to perform a Zig type to C type conversion step I expect this to be much faster. Because of these new typedef gymnastics, I have avoided needing to access a global intern hash map which preserves the existing parallelizability properties of the declaration generation in the C backend. I fully intend to realize further improvements with thisCTypeinfrastructure, but this was the first time it starting passing tests again so it seemed like a good time to get some (human and CI) feedback. One specific future improvement is usingCTypeas the key ofLocalsMapwhich should , but there should be lots of other similar thingsCTypestruct.Typewith converting thatTypeto aCTypeand printing that instead.@tagNameimplementation hacks.zig_u8and friends withstdint.hversions, with hopefully functional fallback code before C99.lib/zig.hwithout needing to updatestage1/zig1.wasmand integrate with theupdate-zig1step.Closes #10061