nvzqz / swift-bindgen

Bridging the gap between Swift and Rust
Apache License 2.0
149 stars 9 forks source link

Swift Function Call ABI #3

Open nvzqz opened 4 years ago

nvzqz commented 4 years ago

For the long-term success of this project, it should allow for exposing functions in Rust that can be called in Swift via the Swift function ABI as well as calling functions declared in Swift directly from Rust without an intermediate C or Objective-C layer. LLVM currently supports the Swift calling convention, also known as swiftcc. In order to expose Swift functions and call them directly in an FFI-safe manner, the Rust compiler needs to be taught Swift's function call ABI.

https://github.com/rust-lang/rust/pull/64582 implements the basic function ABI in rustc via LLVM. This would allow for:

extern "Swift" {
    fn foo();
}

extern "Swift" fn bar() {
    // ...
}

type F = extern "Swift" fn();
Dante-Broggi commented 4 years ago

One thing of note is that there is a definite difference between "the Swift calling convention" and swiftcc, as mentioned in this llvm thread, in particular swiftcc uses the llvm / backend support for struct aggregates, while "the Swift calling convention", pre-expands or indirects all structs before llvm.

A particular example is that these functions are ABI equivalent according to "the Swift calling convention" as stated here in the aforementioned thread.

struct A { i32, float };
struct B { float, i32 };

define @foo (A, i32) -> @foo(i32, float, i32);

// and

define @foo (i32, B) -> @foo(i32, float, i32);