-
-
Notifications
You must be signed in to change notification settings - Fork 46
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/C++ Backend #74
Comments
This proposal sounds amazing! I'd love to help with this sometime I just have a few questions/comments:
Sorry if this was a lot. I'm very excited by the potential Cyber has and would love to help in anyway available! |
Not by default but that can be behind a flag if it can be reasonably done. The code it generates should look like what you might write yourself if you had to follow the same semantics as Cyber such as inserting retain/release ops, handling conversions from primitive to box values, handle exceptions via error codes (which is different from the VM), etc. Variable and function names should try to be very close to the original.
Most definitely not. We'd want to own everything up to machine code generation to have as much flexibility as we can. Last thing we'd want is to implement X feature and realize it's not possible because of a dependency.
Once we can generate the single .c file, splitting it into multiple units or a single header file shouldn't be too much trouble. These options can be behind a flag. In a default build, you wouldn't even see C files unless you actually wanted to.
For C++ the idea is to reuse a lot of the same C code, and only generate the shims (classes) so that it's ergonomic for the C++ side to call into.
AOT is not really meant to be used for the embed API, because the host is usually the AOT program. But... we can reuse the C backend as another JIT method. Definitely slower to compile than the copy-patch JIT but it can trade the slower compilation speed for max performance.
See response about readability. The goal of the first iteration is to make sure it will work and getting tests to pass.
I don't think we will need to... it would be better if we write Cyber code to replace it (at least the non cross platform parts). In the meantime, we'll need to link with the static lib so that we can bootstrap (be able to run test asserts, etc).
Currently FFI is done at runtime. For AOT, we'll need to offer a way to statically/dynamically link libraries too (emitting externs).
I think this would just be a wrapper around the C API.
Most systems languages offer similar semantics so it wouldn't be difficult to do (Rust would be harder). The hard part is making sure Cyber semantics can be accurately represented. After C/C++, I'd be more interested in LLVM or a Zig IR (if they become a competitor to LLVM). Cranelift is also an interesting choice being aligned with WASM. Of course, I wouldn't stop anyone from supporting more... BTW, I've actually started on this as I wanted to see if it was possible and also cause it's interesting... I will push the commits up once I've gotten a good feel for it. |
This is roughly what the generated code looks like for a test case: // Other headers..
i48 main_fib(Fiber* f, i48);
extern Value builtins_typesym(Fiber*, Value*, uint8_t);
extern Value test_eq(Fiber*, Value*, uint8_t);
// Other externs...
int main() {
pm.syms = cy_syms;
mainFiber.pm = ±
Fiber* f = &mainFiber;
i48 res = main_fib(f, 6);
Value tmp1 = TRY_PANIC(test_eq(f, (Value[]){BOX_INT48(res), BOX_INT(8)}, 2));
Value tmp2 = TRY_PANIC(test_eq(f, (Value[]){TRY_PANIC(builtins_typesym(f, (Value[]){BOX_INT48(res)}, 1)), BOX_SYM(10)}, 2));
return 0;
}
i48 main_fib(Fiber* f, i48 n) {
if (n < 2) {
return n;
}
return main_fib(f, n - 1) + main_fib(f, n - 2);
} |
This is something that I won't get to for a long ... long time. But I see value in having a C/C++ backend as our AOT strategy.
This can be fun to contribute to if you want to learn about compilers and still generate code that can be useful by itself.
Some criteria:
Progress:
How to work on this:
zig build lib -Drt=pm
This tells it to build with a physical machine as the runtime instead of the usual VM. Currently each std function is added manually here:cyber/src/lib.zig
Line 27 in 0a57ae4
The library will be built to
zig-out/lib/libcyber.a
.zig build test -Dtest-backend=cc -Dtest-filter="Tests"
This tells it to use systems C compiler but currently it's hardcoded toclang
. The static lib generated above is hardcoded to be linked to the generated C code. Remove the!aot
incyber/test/behavior_test.zig
Line 90 in 0a57ae4
The text was updated successfully, but these errors were encountered: