Changing the ABI of only one function

I'm trying to figure out if Rust supports setting the ABI of only one function to a non-standard one?

What I mean by that is e.g. I have two functions:

fn func1(a: i32, b: i32, c: i32) __attribute__((ms_abi)) {
    println!("{} {} {}", a, b, c);
}

fn func2(a: i32, b: i32, c: i32) {
    println!("{} {} {}", a, b, c);
}

When compiling the code on x64, I'd like to instruct the compiler that when I call func1, I would like to generate the code that passes arguments to rcx (a), rdx (b) and r8 (c) -- in other words, I'd like the function to use the Microsoft ABI, even when compiled on Linux.

But when I call func2, normal Rust ABI should be used (defined by the platform used to compile the code).

I've used the __attribute__((ms_abi)) which doesn't exist in Rust, but that would be what I would use when I would want to do the same in C++ and when compiling using GCC.

Please note that I don't want to change the ABI of all functions in the project, I'd like to change the ABI of just one function and nothing else (so that I can pass a pointer of this function to some other component (which expects the MS ABI), and expect this component to be able to call my function).

Does Rust support setting the function ABI like this?

The syntax is extern "abi-string" fn foo(...). I don't think the compiler will recognize eg. stdcall on Linux, but you can give it a try.

2 Likes

I think this is what I was looking for (extern "win64"), thanks.

https://doc.rust-lang.org/beta/reference/items/external-blocks.html

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.