-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
make translate-c self-hosted #1964
Comments
I did a sort of proof-of-concept with |
I started on step 1 in 356cfa0. The last puzzle piece to fit in here is that, until we get self-hosted and start shipping stage2 (#89), we will have an interesting build process:
I will note this negates the "small stage1 performance price today" I mentioned earlier. We can have our 🍰 and eat it too! |
Marking this as contributor friendly since you can follow the pattern I've started in the latest commit (c3c92ca). The goal is to delete these lines from #include <clang/Frontend/ASTUnit.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/AST/Expr.h> Instead relying only on the decls in |
C API Progress Bar (
|
This modifies the build process of Zig to put all of the source files into libcompiler.a, except main.cpp and userland.cpp. Next, the build process links main.cpp, userland.cpp, and libcompiler.a into zig1. userland.cpp is a shim for functions that will later be replaced with self-hosted implementations. Next, the build process uses zig1 to build src-self-hosted/stage1.zig into libuserland.a, which does not depend on any of the things that are shimmed in userland.cpp, such as translate-c. Finally, the build process re-links main.cpp and libcompiler.a, except with libuserland.a instead of userland.cpp. Now the shims are replaced with .zig code. This provides all of the Zig standard library to the stage1 C++ compiler, and enables us to move certain things to userland, such as translate-c. As a proof of concept I have made the `zig zen` command use text defined in userland. I added `zig translate-c-2` which is a work-in-progress reimplementation of translate-c in userland, which currently calls `std.debug.panic("unimplemented")` and you can see the stack trace makes it all the way back into the C++ main() function (Thanks LemonBoy for improving that!). This could potentially let us move other things into userland, such as hashing algorithms, the entire cache system, .d file parsing, pretty much anything that libuserland.a itself doesn't need to depend on. This can also let us have `zig fmt` in stage1 without the overhead of child process execution, and without the initial compilation delay before it gets cached. See #1964
Now that #2295 is merged, all the proofs of concept are completed, the plan is working, and the rest of this issue is just porting the C++ translate-c code to Zig. We have |
Also breaking std lib API change: the return value of std.zig.parse returns `*ast.Tree` rather than `ast.Tree`. See #1964
Previously, `zig fmt` on the stage1 compiler (which is what we currently ship) would perform what equates to `zig run std/special/fmt_runner.zig` Now, `zig fmt` is implemented with the hybrid zig/C++ strategy outlined by #1964. This means Zig no longer has to ship some of the stage2 .zig files, and there is no longer a delay when running `zig fmt` for the first time.
Progress:
|
Progress:
|
stage1 translate-c actually has this wrong. When exporting a function, it's ok to use empty parameters. But for prototypes, "no prototype" means that it has to be emitted as a function that accepts anything, e.g. extern fn foo(...) void; See #1964
My "get_c_type for vectors" patch allows generating header files for
(trying to translate use of vector types is probably not worth it) |
See #1964 translate_c.cpp now exclusively uses the clang API via zig_clang.h shaves off 5 seconds from building zig when translate_c.cpp (or any h files it uses) change.
Huge thanks to @Vexu for doing most of the porting of translate-c to stage2! This project is now complete. |
Here's the plan:
Currently,
zig translate-c
and@cImport
are implemented with translate_c.cpp andzig translate-c-2
is implemented with translate_c.zig. There will be a separate set of translate-c tests specifically for the Zig implementation, until finally the Zig implementation passes all the tests and step 3 is completed.The text was updated successfully, but these errors were encountered: