Skip to content

Commit e12bc23

Browse files
committed
auto merge of #10527 : eholk/rust/win64, r=alexcrichton
This was needed to access UEFI boot services in my new Boot2Rust experiment. I also realized that Rust functions declared as extern always use the C calling convention regardless of how they were declared, so this pull request fixes that as well.
2 parents 6a25ba3 + 02e565a commit e12bc23

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

src/librustc/lib/llvm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum CallConv {
3333
ColdCallConv = 9,
3434
X86StdcallCallConv = 64,
3535
X86FastcallCallConv = 65,
36+
X86_64_Win64 = 79,
3637
}
3738

3839
pub enum Visibility {

src/librustc/middle/trans/foreign.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use syntax::{ast};
3333
use syntax::{attr, ast_map};
3434
use syntax::parse::token::special_idents;
3535
use syntax::abi::{RustIntrinsic, Rust, Stdcall, Fastcall, System,
36-
Cdecl, Aapcs, C, AbiSet};
36+
Cdecl, Aapcs, C, AbiSet, Win64};
3737
use util::ppaux::{Repr, UserString};
3838
use middle::trans::type_::Type;
3939

@@ -96,6 +96,7 @@ pub fn llvm_calling_convention(ccx: &mut CrateContext,
9696
Stdcall => lib::llvm::X86StdcallCallConv,
9797
Fastcall => lib::llvm::X86FastcallCallConv,
9898
C => lib::llvm::CCallConv,
99+
Win64 => lib::llvm::X86_64_Win64,
99100

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

399400
let tys = foreign_types_for_id(ccx, node_id);
400401
let llfn_ty = lltype_for_fn_from_foreign_types(&tys);
402+
let t = ty::node_id_to_type(ccx.tcx, node_id);
403+
let cconv = match ty::get(t).sty {
404+
ty::ty_bare_fn(ref fn_ty) => {
405+
let c = llvm_calling_convention(ccx, fn_ty.abis);
406+
c.unwrap_or(lib::llvm::CCallConv)
407+
}
408+
_ => lib::llvm::CCallConv
409+
};
401410
let llfn = base::register_fn_llvmty(ccx,
402411
sp,
403412
sym,
404413
node_id,
405-
lib::llvm::CCallConv,
414+
cconv,
406415
llfn_ty);
407416
add_argument_attributes(&tys, llfn);
408417
debug!("register_rust_fn_with_foreign_abi(node_id={:?}, llfn_ty={}, llfn={})",

src/libsyntax/abi.rs

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub enum Abi {
2323
Stdcall,
2424
Fastcall,
2525
Aapcs,
26+
Win64,
2627

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

7780
// Cross-platform ABIs
7881
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Make sure Rust generates the correct calling convention for extern
12+
// functions.
13+
14+
#[inline(never)]
15+
#[cfg(target_arch = "x86_64")]
16+
pub extern "win64" fn foo(a: int, b: int, c: int, d: int) {
17+
assert!(a == 1);
18+
assert!(b == 2);
19+
assert!(c == 3);
20+
assert!(d == 4);
21+
22+
println!("a: {:?}, b: {:?}, c: {:?}, d: {:?}",
23+
a, b, c, d)
24+
}
25+
26+
#[inline(never)]
27+
#[cfg(target_arch = "x86")]
28+
#[cfg(target_arch = "arm")]
29+
pub extern fn foo(a: int, b: int, c: int, d: int) {
30+
assert!(a == 1);
31+
assert!(b == 2);
32+
assert!(c == 3);
33+
assert!(d == 4);
34+
35+
println!("a: {:?}, b: {:?}, c: {:?}, d: {:?}",
36+
a, b, c, d)
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast: aux-build not compatible with fast
12+
// aux-build:extern_calling_convention.rs
13+
14+
extern mod extern_calling_convention;
15+
16+
use extern_calling_convention::foo;
17+
18+
fn main() {
19+
foo(1, 2, 3, 4);
20+
}

0 commit comments

Comments
 (0)