2020-07-31 01:41:25 -05:00
|
|
|
// compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib
|
2020-08-02 10:23:49 -05:00
|
|
|
// needs-llvm-components: avr
|
2020-06-11 00:52:09 -05:00
|
|
|
|
|
|
|
// This test validates that function pointers can be stored in global variables
|
|
|
|
// and called upon. It ensures that Rust emits function pointers in the correct
|
|
|
|
// address space to LLVM so that an assertion error relating to casting is
|
|
|
|
// not triggered.
|
|
|
|
//
|
|
|
|
// It also validates that functions can be called through function pointers
|
|
|
|
// through traits.
|
|
|
|
|
2022-12-11 16:27:01 -06:00
|
|
|
#![feature(no_core, lang_items, intrinsics, unboxed_closures, arbitrary_self_types)]
|
2020-06-11 00:52:09 -05:00
|
|
|
#![crate_type = "lib"]
|
|
|
|
#![no_core]
|
|
|
|
|
|
|
|
#[lang = "sized"]
|
|
|
|
pub trait Sized { }
|
|
|
|
#[lang = "copy"]
|
|
|
|
pub trait Copy { }
|
|
|
|
#[lang = "receiver"]
|
|
|
|
pub trait Receiver { }
|
2022-10-02 14:41:54 -05:00
|
|
|
#[lang = "tuple_trait"]
|
|
|
|
pub trait Tuple { }
|
2020-06-11 00:52:09 -05:00
|
|
|
|
|
|
|
pub struct Result<T, E> { _a: T, _b: E }
|
|
|
|
|
|
|
|
impl Copy for usize {}
|
2020-10-24 19:00:00 -05:00
|
|
|
impl Copy for &usize {}
|
2020-06-11 00:52:09 -05:00
|
|
|
|
|
|
|
#[lang = "drop_in_place"]
|
|
|
|
pub unsafe fn drop_in_place<T: ?Sized>(_: *mut T) {}
|
|
|
|
|
|
|
|
#[lang = "fn_once"]
|
2022-10-02 14:41:54 -05:00
|
|
|
pub trait FnOnce<Args: Tuple> {
|
2020-06-11 00:52:09 -05:00
|
|
|
#[lang = "fn_once_output"]
|
|
|
|
type Output;
|
|
|
|
|
|
|
|
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang = "fn_mut"]
|
2022-10-02 14:41:54 -05:00
|
|
|
pub trait FnMut<Args: Tuple> : FnOnce<Args> {
|
2020-06-11 00:52:09 -05:00
|
|
|
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang = "fn"]
|
2022-10-02 14:41:54 -05:00
|
|
|
pub trait Fn<Args: Tuple>: FnOnce<Args> {
|
2020-06-11 00:52:09 -05:00
|
|
|
/// Performs the call operation.
|
|
|
|
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
2022-12-11 16:27:01 -06:00
|
|
|
extern "rust-intrinsic" {
|
|
|
|
pub fn transmute<Src, Dst>(src: Src) -> Dst;
|
|
|
|
}
|
|
|
|
|
2020-06-11 00:52:09 -05:00
|
|
|
pub static mut STORAGE_FOO: fn(&usize, &mut u32) -> Result<(), ()> = arbitrary_black_box;
|
|
|
|
pub static mut STORAGE_BAR: u32 = 12;
|
|
|
|
|
|
|
|
fn arbitrary_black_box(ptr: &usize, _: &mut u32) -> Result<(), ()> {
|
|
|
|
let raw_ptr = ptr as *const usize;
|
|
|
|
let _v: usize = unsafe { *raw_ptr };
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
#[no_mangle]
|
|
|
|
fn call_through_fn_trait(a: &mut impl Fn<(), Output=()>) {
|
|
|
|
(*a)()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
fn update_bar_value() {
|
|
|
|
unsafe {
|
|
|
|
STORAGE_BAR = 88;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 08:19:56 -05:00
|
|
|
// CHECK: define dso_local void @test(){{.+}}addrspace(1)
|
2020-06-11 00:52:09 -05:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn test() {
|
|
|
|
let mut buf = 7;
|
|
|
|
|
|
|
|
// A call through the Fn trait must use address space 1.
|
|
|
|
//
|
|
|
|
// CHECK: call{{.+}}addrspace(1) void @call_through_fn_trait()
|
|
|
|
call_through_fn_trait(&mut update_bar_value);
|
|
|
|
|
|
|
|
// A call through a global variable must use address space 1.
|
|
|
|
// CHECK: load {{.*}}addrspace(1){{.+}}FOO
|
|
|
|
unsafe {
|
|
|
|
STORAGE_FOO(&1, &mut buf);
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 16:27:01 -06:00
|
|
|
|
|
|
|
// Validate that we can codegen transmutes between data ptrs and fn ptrs.
|
|
|
|
|
2023-07-27 16:44:13 -05:00
|
|
|
// CHECK: define{{.+}}ptr addrspace(1) @transmute_data_ptr_to_fn(ptr{{.*}} %x)
|
2022-12-11 16:27:01 -06:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe fn transmute_data_ptr_to_fn(x: *const ()) -> fn() {
|
|
|
|
// It doesn't matter precisely how this is codegenned (through memory or an addrspacecast),
|
|
|
|
// as long as it doesn't cause a verifier error by using `bitcast`.
|
|
|
|
transmute(x)
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:44:13 -05:00
|
|
|
// CHECK: define{{.+}}ptr @transmute_fn_ptr_to_data(ptr addrspace(1){{.*}} %x)
|
2022-12-11 16:27:01 -06:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe fn transmute_fn_ptr_to_data(x: fn()) -> *const () {
|
|
|
|
// It doesn't matter precisely how this is codegenned (through memory or an addrspacecast),
|
|
|
|
// as long as it doesn't cause a verifier error by using `bitcast`.
|
|
|
|
transmute(x)
|
|
|
|
}
|
2023-01-22 22:03:58 -06:00
|
|
|
|
|
|
|
pub enum Either<T, U> { A(T), B(U) }
|
|
|
|
|
|
|
|
// Previously, we would codegen this as passing/returning a scalar pair of `{ i8, ptr }`,
|
|
|
|
// with the `ptr` field representing both `&i32` and `fn()` depending on the variant.
|
|
|
|
// This is incorrect, because `fn()` should be `ptr addrspace(1)`, not `ptr`.
|
|
|
|
|
2023-07-27 16:44:13 -05:00
|
|
|
// CHECK: define{{.+}}void @should_not_combine_addrspace(ptr{{.+}}sret{{.+}}%_0, ptr{{.+}}%x)
|
2023-01-22 22:03:58 -06:00
|
|
|
#[no_mangle]
|
|
|
|
#[inline(never)]
|
|
|
|
pub fn should_not_combine_addrspace(x: Either<&i32, fn()>) -> Either<&i32, fn()> {
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
|
|
|
// The incorrectness described above would result in us producing (after optimizations)
|
|
|
|
// a `ptrtoint`/`inttoptr` roundtrip to convert from `ptr` to `ptr addrspace(1)`.
|
|
|
|
|
|
|
|
// CHECK-LABEL: @call_with_fn_ptr
|
|
|
|
#[no_mangle]
|
|
|
|
pub fn call_with_fn_ptr<'a>(f: fn()) -> Either<&'a i32, fn()> {
|
|
|
|
// CHECK-NOT: ptrtoint
|
|
|
|
// CHECK-NOT: inttoptr
|
|
|
|
// CHECK: call addrspace(1) void @should_not_combine_addrspace
|
|
|
|
should_not_combine_addrspace(Either::B(f))
|
|
|
|
}
|