]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/compiler_builtins.rs
Merge commit '7d53619064ab7045c383644cb445052d2a3d46db' into sync_cg_clif-2023-02-09
[rust.git] / compiler / rustc_codegen_cranelift / src / compiler_builtins.rs
1 #[cfg(all(unix, feature = "jit"))]
2 use std::ffi::c_int;
3 #[cfg(feature = "jit")]
4 use std::ffi::c_void;
5
6 // FIXME replace with core::ffi::c_size_t once stablized
7 #[allow(non_camel_case_types)]
8 #[cfg(feature = "jit")]
9 type size_t = usize;
10
11 macro_rules! builtin_functions {
12     (
13         $register:ident;
14         $(
15             $(#[$attr:meta])?
16             fn $name:ident($($arg_name:ident: $arg_ty:ty),*) -> $ret_ty:ty;
17         )*
18     ) => {
19         #[cfg(feature = "jit")]
20         #[allow(improper_ctypes)]
21         extern "C" {
22             $(
23                 $(#[$attr])?
24                 fn $name($($arg_name: $arg_ty),*) -> $ret_ty;
25             )*
26         }
27
28         #[cfg(feature = "jit")]
29         pub(crate) fn $register(builder: &mut cranelift_jit::JITBuilder) {
30             for (name, val) in [$($(#[$attr])? (stringify!($name), $name as *const u8)),*] {
31                 builder.symbol(name, val);
32             }
33         }
34     };
35 }
36
37 builtin_functions! {
38     register_functions_for_jit;
39
40     // integers
41     fn __multi3(a: i128, b: i128) -> i128;
42     fn __udivti3(n: u128, d: u128) -> u128;
43     fn __divti3(n: i128, d: i128) -> i128;
44     fn __umodti3(n: u128, d: u128) -> u128;
45     fn __modti3(n: i128, d: i128) -> i128;
46     fn __rust_u128_addo(a: u128, b: u128) -> (u128, bool);
47     fn __rust_i128_addo(a: i128, b: i128) -> (i128, bool);
48     fn __rust_u128_subo(a: u128, b: u128) -> (u128, bool);
49     fn __rust_i128_subo(a: i128, b: i128) -> (i128, bool);
50     fn __rust_u128_mulo(a: u128, b: u128) -> (u128, bool);
51     fn __rust_i128_mulo(a: i128, b: i128) -> (i128, bool);
52
53     // floats
54     fn __floattisf(i: i128) -> f32;
55     fn __floattidf(i: i128) -> f64;
56     fn __floatuntisf(i: u128) -> f32;
57     fn __floatuntidf(i: u128) -> f64;
58     fn __fixsfti(f: f32) -> i128;
59     fn __fixdfti(f: f64) -> i128;
60     fn __fixunssfti(f: f32) -> u128;
61     fn __fixunsdfti(f: f64) -> u128;
62
63     // allocator
64     // NOTE: These need to be mentioned here despite not being part of compiler_builtins because
65     // newer glibc resolve dlsym("malloc") to libc.so despite the override in the rustc binary to
66     // use jemalloc. Libraries opened with dlopen still get the jemalloc version, causing multiple
67     // allocators to be mixed, resulting in a crash.
68     fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
69     #[cfg(unix)]
70     fn posix_memalign(memptr: *mut *mut c_void, align: size_t, size: size_t) -> c_int;
71     fn malloc(size: size_t) -> *mut c_void;
72     fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
73     fn free(p: *mut c_void) -> ();
74
75 }