]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/src/x86.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / libcompiler_builtins / src / x86.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(windows, target_env = "gnu", not(feature = "mangled-names")))]
12 #[naked]
13 #[no_mangle]
14 pub unsafe fn ___chkstk_ms() {
15 asm!("
16 push %ecx
17 push %eax
18 cmp $$0x1000,%eax
19 lea 12(%esp),%ecx
20 jb 1f
21 2:
22 sub $$0x1000,%ecx
23 test %ecx,(%ecx)
24 sub $$0x1000,%eax
25 cmp $$0x1000,%eax
26 ja 2b
27 1:
28 sub %eax,%ecx
29 test %ecx,(%ecx)
30 pop %eax
31 pop %ecx
32 ret" ::: "memory" : "volatile");
33 intrinsics::unreachable();
34 }
35
36 // FIXME: __alloca should be an alias to __chkstk
37 #[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
38 #[naked]
39 #[no_mangle]
40 pub unsafe fn __alloca() {
41 asm!("jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable"
42 ::: "memory" : "volatile");
43 intrinsics::unreachable();
44 }
45
46 #[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
47 #[naked]
48 #[no_mangle]
49 pub unsafe fn ___chkstk() {
50 asm!("
51 push %ecx
52 cmp $$0x1000,%eax
53 lea 8(%esp),%ecx // esp before calling this routine -> ecx
54 jb 1f
55 2:
56 sub $$0x1000,%ecx
57 test %ecx,(%ecx)
58 sub $$0x1000,%eax
59 cmp $$0x1000,%eax
60 ja 2b
61 1:
62 sub %eax,%ecx
63 test %ecx,(%ecx)
64
65 lea 4(%esp),%eax // load pointer to the return address into eax
66 mov %ecx,%esp // install the new top of stack pointer into esp
67 mov -4(%eax),%ecx // restore ecx
68 push (%eax) // push return address onto the stack
69 sub %esp,%eax // restore the original value in eax
70 ret" ::: "memory" : "volatile");
71 intrinsics::unreachable();
72 }