Gankra / abi-cafe

Pair your compilers up at The ABI Cafe!
https://faultlore.com/abi-cafe/book/
216 stars 14 forks source link

Add swift backend? #32

Open Gankra opened 1 month ago

Gankra commented 1 month ago

I'm not sure how much work this is but it sure would be neat!

belkadan commented 1 month ago

Dumping some thoughts here in case you or someone else ever gets to this:

belkadan commented 1 month ago
Gankra commented 1 month ago

As of #39 we've made a pretty significant shift in the """semantics""" of KDLScript to make it more useful for testing non-C-ABIs and layouts. We now take a single type declaration and use it to test both C reprs and Rust reprs, as well as C conventions and Rust conventions. (this is useful for i.e. cranelift which should ideally be ABI-compatible with the llvm-based rustc backend).

Basically at this point it's basically giving you the shape of types and interfaces, and the harness is free to take that shape and apply modifiers like "...with $LANG-style repr" or "...with $LANG calling convention".

So yeah we could have a repr(Swift) and Convention::Swift which reflect what happens when you just Write Some Normal Swift Code and ask it to generate two (swift?)staticlibs that get linked into one (swift?)dylib. And if anyone ever has a wild project to add Swift interop to Rust or whatever then hey we're ready to deal with that!

It's interesting to hear that the header importer is still the only decent way to ask Swift to give things C-style layout, but we could maybe handle that..? I was sure one day we'd need to deal with a staticlib needing multiple source files, but adding that is still gonna be a pain in the ass.

Gankra commented 1 month ago

There's one other exception to this: unlike Rust, in Swift a zero-sized type always has stride = 1. This includes Void/(). So ((), (), (), ()) has total size 0 and stride 1, but the buffer inside [(), (), (), ()] (a heap-allocated array in Swift, closer to Vec) is 4 bytes long.

FWIW this kind of thing is actually awesome/great, for abi-cafe to slam face first into without adjustment. The original problem statement of the tool was to demonstrate these kinds of mismatches for decision-makers! It's still an open question as to what level we should mark these kinds of issues as "known bad" on (as we ran into with the new f16/f128 support), but I think it's broadly desirable to be able to ask for them and have them blow up.