|
| 1 | +#![feature(extended_varargs_abi_support)] |
| 2 | +//@ run-pass |
| 3 | +//@ only-arm-unknown-linux-gnueabihf |
| 4 | + |
| 5 | +// Check that multiple c-variadic calling conventions can be used in the same program. |
| 6 | +// |
| 7 | +// Clang and gcc reject defining functions with a non-default calling convention and a variable |
| 8 | +// argument list, so C programs that use multiple c-variadic calling conventions are unlikely |
| 9 | +// to come up. Here we validate that our codegen backends do in fact generate correct code. |
| 10 | + |
| 11 | +extern "C" { |
| 12 | + fn variadic_c(_: f64, _: ...) -> f64; |
| 13 | +} |
| 14 | + |
| 15 | +extern "aapcs" { |
| 16 | + fn variadic_aapcs(_: f64, _: ...) -> f64; |
| 17 | +} |
| 18 | + |
| 19 | +fn main() { |
| 20 | + unsafe { |
| 21 | + assert_eq!(variadic_c(1.0, 2.0, 3.0), 1.0 + 2.0 + 3.0); |
| 22 | + assert_eq!(variadic_aapcs(1.0, 2.0, 3.0), 1.0 + 2.0 + 3.0); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// This assembly was generated using https://godbolt.org/z/xcW6a1Tj5, and corresponds to the |
| 27 | +// following code compiled for the `armv7-unknown-linux-gnueabihf` target: |
| 28 | +// |
| 29 | +// ```rust |
| 30 | +// #![feature(c_variadic)] |
| 31 | +// |
| 32 | +// #[unsafe(no_mangle)] |
| 33 | +// unsafe extern "C" fn variadic(a: f64, mut args: ...) -> f64 { |
| 34 | +// let b = args.arg::<f64>(); |
| 35 | +// let c = args.arg::<f64>(); |
| 36 | +// |
| 37 | +// a + b + c |
| 38 | +// } |
| 39 | +// ``` |
| 40 | +core::arch::global_asm!( |
| 41 | + r#" |
| 42 | +{variadic_c}: |
| 43 | +{variadic_aapcs}: |
| 44 | + sub sp, sp, #12 |
| 45 | + stmib sp, {{r2, r3}} |
| 46 | + vmov d0, r0, r1 |
| 47 | + add r0, sp, #4 |
| 48 | + vldr d1, [sp, #4] |
| 49 | + add r0, r0, #15 |
| 50 | + bic r0, r0, #7 |
| 51 | + vadd.f64 d0, d0, d1 |
| 52 | + add r1, r0, #8 |
| 53 | + str r1, [sp] |
| 54 | + vldr d1, [r0] |
| 55 | + vadd.f64 d0, d0, d1 |
| 56 | + vmov r0, r1, d0 |
| 57 | + add sp, sp, #12 |
| 58 | + bx lr |
| 59 | + "#, |
| 60 | + variadic_c = sym variadic_c, |
| 61 | + variadic_aapcs = sym variadic_aapcs, |
| 62 | +); |
0 commit comments