Skip to content

CBE: add C type infrastructure#14691

Merged
andrewrk merged 12 commits intoziglang:masterfrom
jacobly0:ctype
Feb 22, 2023
Merged

CBE: add C type infrastructure#14691
andrewrk merged 12 commits intoziglang:masterfrom
jacobly0:ctype

Conversation

@jacobly0
Copy link
Member

@jacobly0 jacobly0 commented Feb 21, 2023

The main motivation for this PR was that in the C backend, the key of the typedef hash maps used to be a (Zig) Type which did not very accurately model C types. Also, the typedef sorting code started to depend on hacks where certain Types were being used for a non-obvious purposes, like Zig pointers "modeling" C forward declarations (???). This introduces a CType struct 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). Unlike Types (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 between CTypes 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 this CType infrastructure, 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 using CType as the key of LocalsMap which should , but there should be lots of other similar things

  • Introduce the CType struct.
  • Replace printing the C type for a Type with converting that Type to a CType and printing that instead.
  • Replace typedef hash maps with ctype hash maps, which replaces the method of emitting C type definitions.
  • Introduce a lazy function hash map which replaces old @tagName implementation hacks.
  • Replace zig_u8 and friends with stdint.h versions, with hopefully functional fallback code before C99.
  • Implement anonymous aggregate types without using hashes in the name (which would have weird circular dependency issues with the new system anyway).
  • Introduce a way to update lib/zig.h without needing to update stage1/zig1.wasm and integrate with the update-zig1 step.
  • Miscellaneous std fixes that were accumulating some bit rot.

Closes #10061

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.
@andrewrk andrewrk merged commit c9e02d3 into ziglang:master Feb 22, 2023
@jacobly0 jacobly0 deleted the ctype branch February 22, 2023 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

improve the C backend lowering of Zig types to C types

3 participants