about summary refs log tree commit diff
path: root/tests/codegen
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-11-29 16:02:20 +0100
committerGitHub <noreply@github.com>2024-11-29 16:02:20 +0100
commit6c9e92268558be0c9cb9449e9999d97dc15ae8aa (patch)
tree542149cc5c5c96a3f4e83602c6056084f3d5c73f /tests/codegen
parent0c4f3a45b86c77b0a89ff06703aa6097af35d924 (diff)
parent67d2f3f6857a1d28113f1a63eb003b53d17088c9 (diff)
downloadrust-6c9e92268558be0c9cb9449e9999d97dc15ae8aa.tar.gz
rust-6c9e92268558be0c9cb9449e9999d97dc15ae8aa.zip
Rollup merge of #131323 - jfrimmel:avr-inline-asm-clobber-abi, r=Amanieu
Support `clobber_abi` in AVR inline assembly

This PR implements the `clobber_abi` part necessary to eventually stabilize the inline assembly for AVR. This is tracked in #93335.
This is heavily inspired by the sibling-PR #131310 for the MSP430. I've explained my reasoning in the first commit message in detail, which is reproduced below for easier reviewing:

This follows the [ABI documentation] of AVR-GCC:

> The [...] call-clobbered general purpose registers (GPRs) are registers that might be destroyed (clobbered) by a function call.
>
> - **R18–R27, R30, R31**
>
>   These GPRs are call clobbered. An ordinary function may use them without restoring the contents. [...]
>
> - **R0, T-Flag**
>
>   The temporary register and the T-flag in SREG are also call-clobbered, but this knowledge is not exposed explicitly to the compiler (R0 is a fixed register).

Therefore this commit lists the aforementioned registers `r18–r27`, `r30` and `r31` as clobbered registers. Since the `r0` register (listed above as well) is not available in inline assembly at all (potentially because the AVR-GCC considers it a fixed register causing the register to never be used in register allocation and LLVM adopting this), there is no need to list it in the clobber list (the `r0`-variant is not even available). A comment was added to ensure, that the `r0` gets added to the clobber-list once the register gets usable in inline ASM.
Since the SREG is normally considered clobbered anyways (unless the user supplies the `preserve_flags`-option), there is no need to explicitly list a bit in this register (which is not possible to list anyways).

Note, that this commit completely ignores the case of interrupts (that are described in the ABI-specification), since every register touched in an ISR need to be saved anyways.

[ABI documentation]: https://gcc.gnu.org/wiki/avr-gcc#Call-Used_Registers

r? ``@Amanieu``

``@rustbot`` label +O-AVR
Diffstat (limited to 'tests/codegen')
-rw-r--r--tests/codegen/asm/avr-clobbers.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/codegen/asm/avr-clobbers.rs b/tests/codegen/asm/avr-clobbers.rs
new file mode 100644
index 00000000000..6e0c75368e2
--- /dev/null
+++ b/tests/codegen/asm/avr-clobbers.rs
@@ -0,0 +1,43 @@
+//@ assembly-output: emit-asm
+//@ compile-flags: --target avr-unknown-gnu-atmega328
+//@ needs-llvm-components: avr
+
+#![crate_type = "rlib"]
+#![feature(no_core, rustc_attrs, lang_items, asm_experimental_arch)]
+#![no_core]
+
+#[lang = "sized"]
+trait Sized {}
+
+#[rustc_builtin_macro]
+macro_rules! asm {
+    () => {};
+}
+
+// CHECK-LABEL: @sreg_is_clobbered
+// CHECK: void asm sideeffect "", "~{sreg}"()
+#[no_mangle]
+pub unsafe fn sreg_is_clobbered() {
+    asm!("", options(nostack, nomem));
+}
+
+// CHECK-LABEL: @sreg_is_not_clobbered_if_preserve_flags_is_used
+// CHECK: void asm sideeffect "", ""()
+#[no_mangle]
+pub unsafe fn sreg_is_not_clobbered_if_preserve_flags_is_used() {
+    asm!("", options(nostack, nomem, preserves_flags));
+}
+
+// CHECK-LABEL: @clobber_abi
+// CHECK: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31},~{sreg}"()
+#[no_mangle]
+pub unsafe fn clobber_abi() {
+    asm!("", clobber_abi("C"), options(nostack, nomem));
+}
+
+// CHECK-LABEL: @clobber_abi_with_preserved_flags
+// CHECK: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31}"()
+#[no_mangle]
+pub unsafe fn clobber_abi_with_preserved_flags() {
+    asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags));
+}