-
Notifications
You must be signed in to change notification settings - Fork 80
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
C backend for Catala #204
Comments
Milestone achieved: I just successfully compiled and ran I diverged significantly from the first draft, heading towards a simplified solution that could be improved later on:
The base types are implemented as such:
There is still a lot to do but this approach seems solid ; in particular, the handling of default terms and exceptions seems to be working. |
(wip branch at https://github.com/CatalaLang/catala/compare/crun) |
@AltGr this is amazing, thanks so much for working out the details of how we get to a working C code. Indeed, this is a change of philosophy from my original draft. I guess your proposal is better in terms of getting to correctness quickly, and I'm glad you switched to it. In thr future though, we might want an alternative C backend way less functional in style where everything is monomorphized, all the data structures flat, allocations minimized and GMP dropped in favor of 128-bit integers and fixed precision points. This alternative backend will be more convenient for interoperability with COBOL codebases... |
Yes, I expected as much, and indeed I think it'll be easier to transition to a lower-level backend from that first working one. It's probably possible to allocate almost everything statically on the stack and use reference passing everywhere, with return values allocated from the parent (GMP is mostly designed this way, although it obviously still requires dynamic allocations): somehow moving from "only allocated pointers" to "no pointers". It'll be harder to get right, though, hence that first version. |
Tracking plans for that "idiomatic" backend in #648 |
Done, we only need to merge PRs now. |
Once #158 is merged, a C backend can easily be implemented for Catala, starting from the Scalc intermediate representation. The code should roughly follow the structure of the Python backend.
Advice from @protz : start with the simple stuff first and then implement optimizations.
Enum translation
They should be translated as tagged unions. Be careful:
null
cannot be distinguished from an integer.Structs
We can use struct literals of C11? But inefficient because call by value, we should call by reference but we have to do memory management (or they're all scoped locally unless your return them as done in the wasm backend of KreMLin).
Closures translation
They should be translated to function pointers after a closure conversion pass is done (closure conversion and hoisting can maybe be implemented in Lcalc).
Integers
Use GMP? But these values are heap allocated, so we need memory management... Checked u64 that error out on overflow? Have two C backends: a high-level C backend and a very low-level backend targeting GPU-isation of code.
The text was updated successfully, but these errors were encountered: