]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/avr/avr-func-addrspace.rs
Auto merge of #105356 - JakobDegen:more-custom-mir, r=oli-obk
[rust.git] / src / test / codegen / avr / avr-func-addrspace.rs
1 // compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib
2 // needs-llvm-components: avr
3
4 // This test validates that function pointers can be stored in global variables
5 // and called upon. It ensures that Rust emits function pointers in the correct
6 // address space to LLVM so that an assertion error relating to casting is
7 // not triggered.
8 //
9 // It also validates that functions can be called through function pointers
10 // through traits.
11
12 #![feature(no_core, lang_items, intrinsics, unboxed_closures, arbitrary_self_types)]
13 #![crate_type = "lib"]
14 #![no_core]
15
16 #[lang = "sized"]
17 pub trait Sized { }
18 #[lang = "copy"]
19 pub trait Copy { }
20 #[lang = "receiver"]
21 pub trait Receiver { }
22 #[lang = "tuple_trait"]
23 pub trait Tuple { }
24
25 pub struct Result<T, E> { _a: T, _b: E }
26
27 impl Copy for usize {}
28 impl Copy for &usize {}
29
30 #[lang = "drop_in_place"]
31 pub unsafe fn drop_in_place<T: ?Sized>(_: *mut T) {}
32
33 #[lang = "fn_once"]
34 pub trait FnOnce<Args: Tuple> {
35     #[lang = "fn_once_output"]
36     type Output;
37
38     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
39 }
40
41 #[lang = "fn_mut"]
42 pub trait FnMut<Args: Tuple> : FnOnce<Args> {
43     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
44 }
45
46 #[lang = "fn"]
47 pub trait Fn<Args: Tuple>: FnOnce<Args> {
48     /// Performs the call operation.
49     extern "rust-call" fn call(&self, args: Args) -> Self::Output;
50 }
51
52 extern "rust-intrinsic" {
53     pub fn transmute<Src, Dst>(src: Src) -> Dst;
54 }
55
56 pub static mut STORAGE_FOO: fn(&usize, &mut u32) -> Result<(), ()> = arbitrary_black_box;
57 pub static mut STORAGE_BAR: u32 = 12;
58
59 fn arbitrary_black_box(ptr: &usize, _: &mut u32) -> Result<(), ()> {
60     let raw_ptr = ptr as *const usize;
61     let _v: usize = unsafe { *raw_ptr };
62     loop {}
63 }
64
65 #[inline(never)]
66 #[no_mangle]
67 fn call_through_fn_trait(a: &mut impl Fn<(), Output=()>) {
68     (*a)()
69 }
70
71 #[inline(never)]
72 fn update_bar_value() {
73     unsafe {
74         STORAGE_BAR = 88;
75     }
76 }
77
78 // CHECK: define dso_local void @test(){{.+}}addrspace(1)
79 #[no_mangle]
80 pub extern "C" fn test() {
81     let mut buf = 7;
82
83     // A call through the Fn trait must use address space 1.
84     //
85     // CHECK: call{{.+}}addrspace(1) void @call_through_fn_trait()
86     call_through_fn_trait(&mut update_bar_value);
87
88     // A call through a global variable must use address space 1.
89     // CHECK: load {{.*}}addrspace(1){{.+}}FOO
90     unsafe {
91         STORAGE_FOO(&1, &mut buf);
92     }
93 }
94
95 // Validate that we can codegen transmutes between data ptrs and fn ptrs.
96
97 // CHECK: define{{.+}}{{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}} @transmute_data_ptr_to_fn({{\{\}\*|ptr}}{{.*}} %x)
98 #[no_mangle]
99 pub unsafe fn transmute_data_ptr_to_fn(x: *const ()) -> fn() {
100     // It doesn't matter precisely how this is codegenned (through memory or an addrspacecast),
101     // as long as it doesn't cause a verifier error by using `bitcast`.
102     transmute(x)
103 }
104
105 // CHECK: define{{.+}}{{\{\}\*|ptr}} @transmute_fn_ptr_to_data({{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}}{{.*}} %x)
106 #[no_mangle]
107 pub unsafe fn transmute_fn_ptr_to_data(x: fn()) -> *const () {
108     // It doesn't matter precisely how this is codegenned (through memory or an addrspacecast),
109     // as long as it doesn't cause a verifier error by using `bitcast`.
110     transmute(x)
111 }