Skip to content

Commit 2a9be64

Browse files
committed
Support AVRTiny devices AVR inline assembly
1 parent 18d8866 commit 2a9be64

File tree

4 files changed

+70
-30
lines changed

4 files changed

+70
-30
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,7 @@ symbols! {
21982198
three_way_compare,
21992199
thumb2,
22002200
thumb_mode: "thumb-mode",
2201+
tinyencoding,
22012202
tmm_reg,
22022203
to_owned_method,
22032204
to_string,

compiler/rustc_target/src/asm/avr.rs

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::fmt;
22

3-
use rustc_span::Symbol;
3+
use rustc_data_structures::fx::FxIndexSet;
4+
use rustc_span::{Symbol, sym};
45

56
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
7+
use crate::spec::{RelocModel, Target};
68

79
def_reg_class! {
810
Avr AvrInlineAsmRegClass {
@@ -52,24 +54,44 @@ impl AvrInlineAsmRegClass {
5254
}
5355
}
5456

57+
pub(crate) fn is_tiny(target_features: &FxIndexSet<Symbol>) -> bool {
58+
target_features.contains(&sym::tinyencoding)
59+
}
60+
61+
fn not_tiny(
62+
_arch: InlineAsmArch,
63+
_reloc_model: RelocModel,
64+
target_features: &FxIndexSet<Symbol>,
65+
_target: &Target,
66+
_is_clobber: bool,
67+
) -> Result<(), &'static str> {
68+
if is_tiny(target_features) {
69+
Err(
70+
"on AVRTiny, r[2-15] are unavailable, r16 (scratch register) and r17 (zero register) are reserved by LLVM",
71+
)
72+
} else {
73+
Ok(())
74+
}
75+
}
76+
5577
def_regs! {
5678
Avr AvrInlineAsmReg AvrInlineAsmRegClass {
57-
r2: reg = ["r2"],
58-
r3: reg = ["r3"],
59-
r4: reg = ["r4"],
60-
r5: reg = ["r5"],
61-
r6: reg = ["r6"],
62-
r7: reg = ["r7"],
63-
r8: reg = ["r8"],
64-
r9: reg = ["r9"],
65-
r10: reg = ["r10"],
66-
r11: reg = ["r11"],
67-
r12: reg = ["r12"],
68-
r13: reg = ["r13"],
69-
r14: reg = ["r14"],
70-
r15: reg = ["r15"],
71-
r16: reg, reg_upper = ["r16"],
72-
r17: reg, reg_upper = ["r17"],
79+
r2: reg = ["r2"] % not_tiny,
80+
r3: reg = ["r3"] % not_tiny,
81+
r4: reg = ["r4"] % not_tiny,
82+
r5: reg = ["r5"] % not_tiny,
83+
r6: reg = ["r6"] % not_tiny,
84+
r7: reg = ["r7"] % not_tiny,
85+
r8: reg = ["r8"] % not_tiny,
86+
r9: reg = ["r9"] % not_tiny,
87+
r10: reg = ["r10"] % not_tiny,
88+
r11: reg = ["r11"] % not_tiny,
89+
r12: reg = ["r12"] % not_tiny,
90+
r13: reg = ["r13"] % not_tiny,
91+
r14: reg = ["r14"] % not_tiny,
92+
r15: reg = ["r15"] % not_tiny,
93+
r16: reg, reg_upper = ["r16"] % not_tiny,
94+
r17: reg, reg_upper = ["r17"] % not_tiny,
7395
r18: reg, reg_upper = ["r18"],
7496
r19: reg, reg_upper = ["r19"],
7597
r20: reg, reg_upper = ["r20"],
@@ -83,14 +105,14 @@ def_regs! {
83105
r30: reg, reg_upper = ["r30", "ZL"],
84106
r31: reg, reg_upper = ["r31", "ZH"],
85107

86-
r3r2: reg_pair = ["r3r2"],
87-
r5r4: reg_pair = ["r5r4"],
88-
r7r6: reg_pair = ["r7r6"],
89-
r9r8: reg_pair = ["r9r8"],
90-
r11r10: reg_pair = ["r11r10"],
91-
r13r12: reg_pair = ["r13r12"],
92-
r15r14: reg_pair = ["r15r14"],
93-
r17r16: reg_pair = ["r17r16"],
108+
r3r2: reg_pair = ["r3r2"] % not_tiny,
109+
r5r4: reg_pair = ["r5r4"] % not_tiny,
110+
r7r6: reg_pair = ["r7r6"] % not_tiny,
111+
r9r8: reg_pair = ["r9r8"] % not_tiny,
112+
r11r10: reg_pair = ["r11r10"] % not_tiny,
113+
r13r12: reg_pair = ["r13r12"] % not_tiny,
114+
r15r14: reg_pair = ["r15r14"] % not_tiny,
115+
r17r16: reg_pair = ["r17r16"] % not_tiny,
94116
r19r18: reg_pair = ["r19r18"],
95117
r21r20: reg_pair = ["r21r20"],
96118
r23r22: reg_pair = ["r23r22"],

compiler/rustc_target/src/asm/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ pub enum InlineAsmClobberAbi {
935935
AArch64NoX18,
936936
Arm64EC,
937937
Avr,
938+
AvrTiny,
938939
RiscV,
939940
RiscVE,
940941
LoongArch,
@@ -995,7 +996,11 @@ impl InlineAsmClobberAbi {
995996
_ => Err(&["C", "system", "efiapi"]),
996997
},
997998
InlineAsmArch::Avr => match name {
998-
"C" | "system" => Ok(InlineAsmClobberAbi::Avr),
999+
"C" | "system" => Ok(if avr::is_tiny(target_features) {
1000+
InlineAsmClobberAbi::AvrTiny
1001+
} else {
1002+
InlineAsmClobberAbi::Avr
1003+
}),
9991004
_ => Err(&["C", "system"]),
10001005
},
10011006
InlineAsmArch::LoongArch32 | InlineAsmArch::LoongArch64 => match name {
@@ -1166,6 +1171,13 @@ impl InlineAsmClobberAbi {
11661171
// is used.
11671172
}
11681173
},
1174+
InlineAsmClobberAbi::AvrTiny => clobbered_regs! {
1175+
Avr AvrInlineAsmReg {
1176+
// Refs: https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny
1177+
1178+
r20, r21, r22, r23, r24, r25, r26, r27, r30, r31,
1179+
}
1180+
},
11691181
InlineAsmClobberAbi::RiscV => clobbered_regs! {
11701182
RiscV RiscVInlineAsmReg {
11711183
// ra

tests/codegen-llvm/asm/avr-clobbers.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//@ add-core-stubs
22
//@ assembly-output: emit-asm
3-
//@ compile-flags: --target avr-none -C target-cpu=atmega328p
4-
//@ needs-llvm-components: avr
3+
//@ revisions: avr avrtiny
4+
//@[avr] compile-flags: --target avr-none -C target-cpu=atmega328p
5+
//@[avr] needs-llvm-components: avr
6+
//@[avrtiny] compile-flags: --target avr-none -C target-cpu=attiny104
7+
//@[avrtiny] needs-llvm-components: avr
58

69
#![crate_type = "rlib"]
710
#![feature(no_core, asm_experimental_arch)]
@@ -25,14 +28,16 @@ pub unsafe fn sreg_is_not_clobbered_if_preserve_flags_is_used() {
2528
}
2629

2730
// CHECK-LABEL: @clobber_abi
28-
// CHECK: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31},~{sreg}"()
31+
// avr: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31},~{sreg}"()
32+
// avrtiny: asm sideeffect "", "={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31},~{sreg}"()
2933
#[no_mangle]
3034
pub unsafe fn clobber_abi() {
3135
asm!("", clobber_abi("C"), options(nostack, nomem));
3236
}
3337

3438
// CHECK-LABEL: @clobber_abi_with_preserved_flags
35-
// CHECK: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31}"()
39+
// avr: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31}"()
40+
// avrtiny: asm sideeffect "", "={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31}"()
3641
#[no_mangle]
3742
pub unsafe fn clobber_abi_with_preserved_flags() {
3843
asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags));

0 commit comments

Comments
 (0)