Skip to content

Correct wasm32 ABI #1952

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 42 additions & 19 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,7 @@ impl MethodCodegen for Method {

let function_name = ctx.rust_ident(function_item.canonical_name(ctx));
let mut args = utils::fnsig_arguments(ctx, signature);
let mut ret = utils::fnsig_return_ty(ctx, signature);
let (mut ret, ..) = utils::fnsig_return_ty(ctx, signature);

if !self.is_static() && !self.is_constructor() {
args[0] = if self.is_const() {
Expand Down Expand Up @@ -3669,7 +3669,7 @@ impl TryToRustTy for FunctionSig {
_: &(),
) -> error::Result<proc_macro2::TokenStream> {
// TODO: we might want to consider ignoring the reference return value.
let ret = utils::fnsig_return_ty(ctx, &self);
let (ret, ret_is_out_param) = utils::fnsig_return_ty(ctx, &self);
let arguments = utils::fnsig_arguments(ctx, &self);
let abi = self.abi();

Expand All @@ -3678,8 +3678,14 @@ impl TryToRustTy for FunctionSig {
warn!("Skipping function with thiscall ABI that isn't supported by the configured Rust target");
Ok(proc_macro2::TokenStream::new())
}
_ => Ok(quote! {
unsafe extern #abi fn ( #( #arguments ),* ) #ret
_ => Ok(if ret_is_out_param {
quote! {
unsafe extern #abi fn ( #( #arguments , )* _: *mut #ret )
}
} else {
quote! {
unsafe extern #abi fn ( #( #arguments ),* ) #ret
}
}),
}
}
Expand Down Expand Up @@ -3746,7 +3752,7 @@ impl CodeGenerator for Function {
};

let args = utils::fnsig_arguments(ctx, signature);
let ret = utils::fnsig_return_ty(ctx, signature);
let (ret, ret_is_out_param) = utils::fnsig_return_ty(ctx, signature);

let mut attributes = vec![];

Expand Down Expand Up @@ -3803,11 +3809,21 @@ impl CodeGenerator for Function {
});

let ident = ctx.rust_ident(canonical_name);
let tokens = quote! {
#wasm_link_attribute
extern #abi {
#(#attributes)*
pub fn #ident ( #( #args ),* ) #ret;
let tokens = if ret_is_out_param {
quote! {
#wasm_link_attribute
extern #abi {
#(#attributes)*
pub fn #ident ( #( #args , )* _: *mut #ret );
}
}
} else {
quote! {
#wasm_link_attribute
extern #abi {
#(#attributes)*
pub fn #ident ( #( #args ),* ) #ret;
}
}
};

Expand Down Expand Up @@ -3845,7 +3861,7 @@ fn objc_method_codegen(
) -> proc_macro2::TokenStream {
let signature = method.signature();
let fn_args = utils::fnsig_arguments(ctx, signature);
let fn_ret = utils::fnsig_return_ty(ctx, signature);
let (fn_ret, ..) = utils::fnsig_return_ty(ctx, signature);

let sig = if method.is_class_method() {
let fn_args = fn_args.clone();
Expand Down Expand Up @@ -4454,15 +4470,17 @@ pub mod utils {
pub fn fnsig_return_ty(
ctx: &BindgenContext,
sig: &FunctionSig,
) -> proc_macro2::TokenStream {
) -> (proc_macro2::TokenStream, bool) {
let return_item = ctx.resolve_item(sig.return_type());
if let TypeKind::Void = *return_item.kind().expect_type().kind() {
quote! {}
let return_ty = return_item.kind().expect_type();
if return_ty.needs_wasm32_abi_hack(ctx) {
let ret_ty = return_item.to_rust_ty_or_opaque(ctx, &());
(ret_ty, true)
} else if let TypeKind::Void = *return_ty.kind() {
(quote! {}, false)
} else {
let ret_ty = return_item.to_rust_ty_or_opaque(ctx, &());
quote! {
-> #ret_ty
}
(quote! { -> #ret_ty }, false)
}
}

Expand All @@ -4488,7 +4506,8 @@ pub mod utils {
// the array type derivation.
//
// [1]: http://c0x.coding-guidelines.com/6.7.5.3.html
let arg_ty = match *arg_ty.canonical_type(ctx).kind() {
let mut arg_ty_tokens = match *arg_ty.canonical_type(ctx).kind()
{
TypeKind::Array(t, _) => {
let stream =
if ctx.options().array_pointers_in_arguments {
Expand All @@ -4515,6 +4534,10 @@ pub mod utils {
_ => arg_item.to_rust_ty_or_opaque(ctx, &()),
};

if arg_ty.needs_wasm32_abi_hack(ctx) {
arg_ty_tokens = quote! { *mut #arg_ty_tokens };
}

let arg_name = match *name {
Some(ref name) => ctx.rust_mangle(name).into_owned(),
None => {
Expand All @@ -4527,7 +4550,7 @@ pub mod utils {
let arg_name = ctx.rust_ident(arg_name);

quote! {
#arg_name : #arg_ty
#arg_name : #arg_ty_tokens
}
})
.collect::<Vec<_>>();
Expand Down
10 changes: 10 additions & 0 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,16 @@ If you encounter an error missing from this list, please file an issue or a PR!"
}
}

pub(crate) fn is_target_wasm32_not_emscripten(&self) -> bool {
match self.target_info {
Some(ref ti) => {
ti.triple.starts_with("wasm32-") &&
!ti.triple.ends_with("emscripten")
}
None => false,
}
}

/// Creates a timer for the current bindgen phase. If time_phases is `true`,
/// the timer will print to stderr when it is dropped, otherwise it will do
/// nothing.
Expand Down
12 changes: 12 additions & 0 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ impl Type {
_ => false,
}
}

pub(crate) fn needs_wasm32_abi_hack(&self, ctx: &BindgenContext) -> bool {
ctx.is_target_wasm32_not_emscripten() &&
match self.kind {
TypeKind::ResolvedTypeRef(id) |
TypeKind::Alias(id) => {
ctx.resolve_type(id).needs_wasm32_abi_hack(ctx)
}
TypeKind::Comp(_) => true,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what to do here. compinfo::layout() is none, compinfo.fields() -> field::layout() is none, etc. How do I figure out the size and shape of this comp?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want self.layout(ctx), compinfo::layout() is just a fallback if we can't get the right size from llvm.

_ => false,
}
}
}

impl IsOpaque for Type {
Expand Down
9 changes: 9 additions & 0 deletions test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct Foo {
unsigned int a;
double b;
};

typedef struct Foo Foo;

void takes_foo(Foo);
Foo gives_foo(void);