Skip to content

Commit 74cd512

Browse files
author
Jorge Aparicio
authored
Merge pull request #40 from mattico/add-x86_64
Add x86_64 builtins
2 parents 999d82a + 6b7a003 commit 74cd512

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ See [rust-lang/rust#35437][0].
133133
- [x] udivsi3.c
134134
- [x] umoddi3.c
135135
- [x] umodsi3.c
136-
- [ ] x86_64/chkstk.S
137-
- [ ] x86_64/chkstk2.S
136+
- [x] x86_64/chkstk.S
137+
- [x] x86_64/chkstk2.S
138138

139139
These builtins are needed to support 128-bit integers, which are in the process of being added to Rust.
140140

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ extern crate rlibc;
2323
#[cfg(target_arch = "arm")]
2424
pub mod arm;
2525

26+
#[cfg(target_arch = "x86_64")]
27+
pub mod x86_64;
28+
2629
pub mod udiv;
2730
pub mod mul;
2831
pub mod shift;

src/x86_64.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use core::intrinsics;
2+
3+
// NOTE These functions are implemented using assembly because they using a custom
4+
// calling convention which can't be implemented using a normal Rust function
5+
6+
// NOTE These functions are never mangled as they are not tested against compiler-rt
7+
// and mangling ___chkstk would break the `jmp ___chkstk` instruction in __alloca
8+
9+
#[cfg(windows)]
10+
#[naked]
11+
#[no_mangle]
12+
pub unsafe fn ___chkstk_ms() {
13+
asm!("push %rcx
14+
push %rax
15+
cmp $$0x1000,%rax
16+
lea 24(%rsp),%rcx
17+
jb 1f
18+
2:
19+
sub $$0x1000,%rcx
20+
test %rcx,(%rcx)
21+
sub $$0x1000,%rax
22+
cmp $$0x1000,%rax
23+
ja 2b
24+
1:
25+
sub %rax,%rcx
26+
test %rcx,(%rcx)
27+
pop %rax
28+
pop %rcx
29+
ret");
30+
intrinsics::unreachable();
31+
}
32+
33+
#[cfg(windows)]
34+
#[naked]
35+
#[no_mangle]
36+
pub unsafe fn __alloca() {
37+
asm!("mov %rcx,%rax // x64 _alloca is a normal function with parameter in rcx
38+
jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable");
39+
intrinsics::unreachable();
40+
}
41+
42+
#[cfg(windows)]
43+
#[naked]
44+
#[no_mangle]
45+
pub unsafe fn ___chkstk() {
46+
asm!("push %rcx
47+
cmp $$0x1000,%rax
48+
lea 16(%rsp),%rcx // rsp before calling this routine -> rcx
49+
jb 1f
50+
2:
51+
sub $$0x1000,%rcx
52+
test %rcx,(%rcx)
53+
sub $$0x1000,%rax
54+
cmp $$0x1000,%rax
55+
ja 2b
56+
1:
57+
sub %rax,%rcx
58+
test %rcx,(%rcx)
59+
60+
lea 8(%rsp),%rax // load pointer to the return address into rax
61+
mov %rcx,%rsp // install the new top of stack pointer into rsp
62+
mov -8(%rax),%rcx // restore rcx
63+
push (%rax) // push return address onto the stack
64+
sub %rsp,%rax // restore the original value in rax
65+
ret");
66+
intrinsics::unreachable();
67+
}
68+

0 commit comments

Comments
 (0)