]> git.proxmox.com Git - rustc.git/blob - vendor/compiler_builtins/src/x86_64.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / vendor / compiler_builtins / src / x86_64.rs
1 #![allow(unused_imports)]
2
3 use core::intrinsics;
4
5 // NOTE These functions are implemented using assembly because they using a custom
6 // calling convention which can't be implemented using a normal Rust function
7
8 // NOTE These functions are never mangled as they are not tested against compiler-rt
9 // and mangling ___chkstk would break the `jmp ___chkstk` instruction in __alloca
10
11 #[cfg(all(
12 windows,
13 target_env = "gnu",
14 not(feature = "no-asm"),
15 not(feature = "mangled-names")
16 ))]
17 #[naked]
18 #[no_mangle]
19 pub unsafe fn ___chkstk_ms() {
20 llvm_asm!("
21 push %rcx
22 push %rax
23 cmp $$0x1000,%rax
24 lea 24(%rsp),%rcx
25 jb 1f
26 2:
27 sub $$0x1000,%rcx
28 test %rcx,(%rcx)
29 sub $$0x1000,%rax
30 cmp $$0x1000,%rax
31 ja 2b
32 1:
33 sub %rax,%rcx
34 test %rcx,(%rcx)
35 pop %rax
36 pop %rcx
37 ret" ::: "memory" : "volatile");
38 intrinsics::unreachable();
39 }
40
41 #[cfg(all(
42 windows,
43 target_env = "gnu",
44 not(feature = "no-asm"),
45 not(feature = "mangled-names")
46 ))]
47 #[naked]
48 #[no_mangle]
49 pub unsafe fn __alloca() {
50 llvm_asm!("mov %rcx,%rax // x64 _alloca is a normal function with parameter in rcx
51 jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable"
52 ::: "memory" : "volatile");
53 intrinsics::unreachable();
54 }
55
56 #[cfg(all(
57 windows,
58 target_env = "gnu",
59 not(feature = "no-asm"),
60 not(feature = "mangled-names")
61 ))]
62 #[naked]
63 #[no_mangle]
64 pub unsafe fn ___chkstk() {
65 llvm_asm!(
66 "
67 push %rcx
68 cmp $$0x1000,%rax
69 lea 16(%rsp),%rcx // rsp before calling this routine -> rcx
70 jb 1f
71 2:
72 sub $$0x1000,%rcx
73 test %rcx,(%rcx)
74 sub $$0x1000,%rax
75 cmp $$0x1000,%rax
76 ja 2b
77 1:
78 sub %rax,%rcx
79 test %rcx,(%rcx)
80
81 lea 8(%rsp),%rax // load pointer to the return address into rax
82 mov %rcx,%rsp // install the new top of stack pointer into rsp
83 mov -8(%rax),%rcx // restore rcx
84 push (%rax) // push return address onto the stack
85 sub %rsp,%rax // restore the original value in rax
86 ret"
87 ::: "memory" : "volatile"
88 );
89 intrinsics::unreachable();
90 }
91
92 // HACK(https://github.com/rust-lang/rust/issues/62785): x86_64-unknown-uefi needs special LLVM
93 // support unless we emit the _fltused
94 #[no_mangle]
95 #[used]
96 #[cfg(target_os = "uefi")]
97 static _fltused: i32 = 0;