Skip to content

Add support for the win64 calling convention #10527

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

Merged
merged 3 commits into from
Nov 20, 2013
Merged
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
1 change: 1 addition & 0 deletions src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum CallConv {
ColdCallConv = 9,
X86StdcallCallConv = 64,
X86FastcallCallConv = 65,
X86_64_Win64 = 79,
}

pub enum Visibility {
Expand Down
13 changes: 11 additions & 2 deletions src/librustc/middle/trans/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use syntax::{ast};
use syntax::{attr, ast_map};
use syntax::parse::token::special_idents;
use syntax::abi::{RustIntrinsic, Rust, Stdcall, Fastcall, System,
Cdecl, Aapcs, C, AbiSet};
Cdecl, Aapcs, C, AbiSet, Win64};
use util::ppaux::{Repr, UserString};
use middle::trans::type_::Type;

Expand Down Expand Up @@ -96,6 +96,7 @@ pub fn llvm_calling_convention(ccx: &mut CrateContext,
Stdcall => lib::llvm::X86StdcallCallConv,
Fastcall => lib::llvm::X86FastcallCallConv,
C => lib::llvm::CCallConv,
Win64 => lib::llvm::X86_64_Win64,

// NOTE These API constants ought to be more specific
Cdecl => lib::llvm::CCallConv,
Expand Down Expand Up @@ -398,11 +399,19 @@ pub fn register_rust_fn_with_foreign_abi(ccx: @mut CrateContext,

let tys = foreign_types_for_id(ccx, node_id);
let llfn_ty = lltype_for_fn_from_foreign_types(&tys);
let t = ty::node_id_to_type(ccx.tcx, node_id);
let cconv = match ty::get(t).sty {
ty::ty_bare_fn(ref fn_ty) => {
let c = llvm_calling_convention(ccx, fn_ty.abis);
c.unwrap_or(lib::llvm::CCallConv)
}
_ => lib::llvm::CCallConv
};
let llfn = base::register_fn_llvmty(ccx,
sp,
sym,
node_id,
lib::llvm::CCallConv,
cconv,
llfn_ty);
add_argument_attributes(&tys, llfn);
debug!("register_rust_fn_with_foreign_abi(node_id={:?}, llfn_ty={}, llfn={})",
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Abi {
Stdcall,
Fastcall,
Aapcs,
Win64,

// Multiplatform ABIs second
Rust,
Expand Down Expand Up @@ -73,6 +74,8 @@ static AbiDatas: &'static [AbiData] = &[
AbiData {abi: Stdcall, name: "stdcall", abi_arch: Archs(IntelBits)},
AbiData {abi: Fastcall, name:"fastcall", abi_arch: Archs(IntelBits)},
AbiData {abi: Aapcs, name: "aapcs", abi_arch: Archs(ArmBits)},
AbiData {abi: Win64, name: "win64",
abi_arch: Archs(1 << (X86_64 as uint))},

// Cross-platform ABIs
//
Expand Down
37 changes: 37 additions & 0 deletions src/test/auxiliary/extern_calling_convention.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Make sure Rust generates the correct calling convention for extern
// functions.

#[inline(never)]
#[cfg(target_arch = "x86_64")]
pub extern "win64" fn foo(a: int, b: int, c: int, d: int) {
assert!(a == 1);
assert!(b == 2);
assert!(c == 3);
assert!(d == 4);

println!("a: {:?}, b: {:?}, c: {:?}, d: {:?}",
a, b, c, d)
}

#[inline(never)]
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "arm")]
pub extern fn foo(a: int, b: int, c: int, d: int) {
assert!(a == 1);
assert!(b == 2);
assert!(c == 3);
assert!(d == 4);

println!("a: {:?}, b: {:?}, c: {:?}, d: {:?}",
a, b, c, d)
}
20 changes: 20 additions & 0 deletions src/test/run-pass/extern-calling-convention-test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-fast: aux-build not compatible with fast
// aux-build:extern_calling_convention.rs

extern mod extern_calling_convention;

use extern_calling_convention::foo;

fn main() {
foo(1, 2, 3, 4);
}