Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ declare_features! (
(unstable, aarch64_ver_target_feature, "1.27.0", Some(44839)),
(unstable, apx_target_feature, "1.88.0", Some(139284)),
(unstable, arm_target_feature, "1.27.0", Some(44839)),
(unstable, avr_target_feature, "CURRENT_RUSTC_VERSION", Some(146889)),
(unstable, bpf_target_feature, "1.54.0", Some(44839)),
(unstable, csky_target_feature, "1.73.0", Some(44839)),
(unstable, ermsb_target_feature, "1.49.0", Some(44839)),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ symbols! {
autodiff_reverse,
automatically_derived,
available_externally,
avr_target_feature,
avx,
avx10_target_feature,
avx512_target_feature,
Expand Down Expand Up @@ -2197,6 +2198,7 @@ symbols! {
three_way_compare,
thumb2,
thumb_mode: "thumb-mode",
tinyencoding,
tmm_reg,
to_owned_method,
to_string,
Expand Down
72 changes: 47 additions & 25 deletions compiler/rustc_target/src/asm/avr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fmt;

use rustc_span::Symbol;
use rustc_data_structures::fx::FxIndexSet;
use rustc_span::{Symbol, sym};

use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use crate::spec::{RelocModel, Target};

def_reg_class! {
Avr AvrInlineAsmRegClass {
Expand Down Expand Up @@ -52,24 +54,44 @@ impl AvrInlineAsmRegClass {
}
}

pub(crate) fn is_tiny(target_features: &FxIndexSet<Symbol>) -> bool {
target_features.contains(&sym::tinyencoding)
}

fn not_tiny(
_arch: InlineAsmArch,
_reloc_model: RelocModel,
target_features: &FxIndexSet<Symbol>,
_target: &Target,
_is_clobber: bool,
) -> Result<(), &'static str> {
if is_tiny(target_features) {
Err(
"on AVRTiny, r[2-15] are unavailable, r16 (scratch register) and r17 (zero register) are reserved by LLVM",
)
} else {
Ok(())
}
}

def_regs! {
Avr AvrInlineAsmReg AvrInlineAsmRegClass {
r2: reg = ["r2"],
r3: reg = ["r3"],
r4: reg = ["r4"],
r5: reg = ["r5"],
r6: reg = ["r6"],
r7: reg = ["r7"],
r8: reg = ["r8"],
r9: reg = ["r9"],
r10: reg = ["r10"],
r11: reg = ["r11"],
r12: reg = ["r12"],
r13: reg = ["r13"],
r14: reg = ["r14"],
r15: reg = ["r15"],
r16: reg, reg_upper = ["r16"],
r17: reg, reg_upper = ["r17"],
r2: reg = ["r2"] % not_tiny,
r3: reg = ["r3"] % not_tiny,
r4: reg = ["r4"] % not_tiny,
r5: reg = ["r5"] % not_tiny,
r6: reg = ["r6"] % not_tiny,
r7: reg = ["r7"] % not_tiny,
r8: reg = ["r8"] % not_tiny,
r9: reg = ["r9"] % not_tiny,
r10: reg = ["r10"] % not_tiny,
r11: reg = ["r11"] % not_tiny,
r12: reg = ["r12"] % not_tiny,
r13: reg = ["r13"] % not_tiny,
r14: reg = ["r14"] % not_tiny,
r15: reg = ["r15"] % not_tiny,
r16: reg, reg_upper = ["r16"] % not_tiny,
r17: reg, reg_upper = ["r17"] % not_tiny,
r18: reg, reg_upper = ["r18"],
r19: reg, reg_upper = ["r19"],
r20: reg, reg_upper = ["r20"],
Expand All @@ -83,14 +105,14 @@ def_regs! {
r30: reg, reg_upper = ["r30", "ZL"],
r31: reg, reg_upper = ["r31", "ZH"],

r3r2: reg_pair = ["r3r2"],
r5r4: reg_pair = ["r5r4"],
r7r6: reg_pair = ["r7r6"],
r9r8: reg_pair = ["r9r8"],
r11r10: reg_pair = ["r11r10"],
r13r12: reg_pair = ["r13r12"],
r15r14: reg_pair = ["r15r14"],
r17r16: reg_pair = ["r17r16"],
r3r2: reg_pair = ["r3r2"] % not_tiny,
r5r4: reg_pair = ["r5r4"] % not_tiny,
r7r6: reg_pair = ["r7r6"] % not_tiny,
r9r8: reg_pair = ["r9r8"] % not_tiny,
r11r10: reg_pair = ["r11r10"] % not_tiny,
r13r12: reg_pair = ["r13r12"] % not_tiny,
r15r14: reg_pair = ["r15r14"] % not_tiny,
r17r16: reg_pair = ["r17r16"] % not_tiny,
r19r18: reg_pair = ["r19r18"],
r21r20: reg_pair = ["r21r20"],
r23r22: reg_pair = ["r23r22"],
Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_target/src/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ pub enum InlineAsmClobberAbi {
AArch64NoX18,
Arm64EC,
Avr,
AvrTiny,
RiscV,
RiscVE,
LoongArch,
Expand Down Expand Up @@ -995,7 +996,11 @@ impl InlineAsmClobberAbi {
_ => Err(&["C", "system", "efiapi"]),
},
InlineAsmArch::Avr => match name {
"C" | "system" => Ok(InlineAsmClobberAbi::Avr),
"C" | "system" => Ok(if avr::is_tiny(target_features) {
InlineAsmClobberAbi::AvrTiny
} else {
InlineAsmClobberAbi::Avr
}),
_ => Err(&["C", "system"]),
},
InlineAsmArch::LoongArch32 | InlineAsmArch::LoongArch64 => match name {
Expand Down Expand Up @@ -1166,6 +1171,13 @@ impl InlineAsmClobberAbi {
// is used.
}
},
InlineAsmClobberAbi::AvrTiny => clobbered_regs! {
Avr AvrInlineAsmReg {
// Refs: https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny

r20, r21, r22, r23, r24, r25, r26, r27, r30, r31,
}
},
InlineAsmClobberAbi::RiscV => clobbered_regs! {
RiscV RiscVInlineAsmReg {
// ra
Expand Down
26 changes: 25 additions & 1 deletion compiler/rustc_target/src/target_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,28 @@ static M68K_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
// tidy-alphabetical-end
];

static AVR_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
// tidy-alphabetical-start
("addsubiw", Unstable(sym::avr_target_feature), &[]),
("break", Unstable(sym::avr_target_feature), &[]),
("eijmpcall", Unstable(sym::avr_target_feature), &[]),
("elpm", Unstable(sym::avr_target_feature), &[]),
("elpmx", Unstable(sym::avr_target_feature), &[]),
("ijmpcall", Unstable(sym::avr_target_feature), &[]),
("jmpcall", Unstable(sym::avr_target_feature), &[]),
("lowbytefirst", Unstable(sym::avr_target_feature), &[]),
("lpm", Unstable(sym::avr_target_feature), &[]),
("lpmx", Unstable(sym::avr_target_feature), &[]),
("movw", Unstable(sym::avr_target_feature), &[]),
("mul", Unstable(sym::avr_target_feature), &[]),
("rmw", Unstable(sym::avr_target_feature), &[]),
("spm", Unstable(sym::avr_target_feature), &[]),
("spmx", Unstable(sym::avr_target_feature), &[]),
("sram", Unstable(sym::avr_target_feature), &[]),
("tinyencoding", Unstable(sym::avr_target_feature), &[]),
// tidy-alphabetical-end
];

/// When rustdoc is running, provide a list of all known features so that all their respective
/// primitives may be documented.
///
Expand All @@ -905,6 +927,7 @@ pub fn all_rust_features() -> impl Iterator<Item = (&'static str, Stability)> {
.chain(IBMZ_FEATURES)
.chain(SPARC_FEATURES)
.chain(M68K_FEATURES)
.chain(AVR_FEATURES)
.cloned()
.map(|(f, s, _)| (f, s))
}
Expand Down Expand Up @@ -972,6 +995,7 @@ impl Target {
"s390x" => IBMZ_FEATURES,
"sparc" | "sparc64" => SPARC_FEATURES,
"m68k" => M68K_FEATURES,
"avr" => AVR_FEATURES,
_ => &[],
}
}
Expand All @@ -989,7 +1013,7 @@ impl Target {
"sparc" | "sparc64" => SPARC_FEATURES_FOR_CORRECT_VECTOR_ABI,
"hexagon" => HEXAGON_FEATURES_FOR_CORRECT_VECTOR_ABI,
"mips" | "mips32r6" | "mips64" | "mips64r6" => MIPS_FEATURES_FOR_CORRECT_VECTOR_ABI,
"nvptx64" | "bpf" | "m68k" => &[], // no vector ABI
"nvptx64" | "bpf" | "m68k" | "avr" => &[], // no vector ABI
"csky" => CSKY_FEATURES_FOR_CORRECT_VECTOR_ABI,
// FIXME: for some tier3 targets, we are overly cautious and always give warnings
// when passing args in vector registers.
Expand Down
14 changes: 10 additions & 4 deletions tests/codegen-llvm/asm/avr-clobbers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//@ add-core-stubs
//@ assembly-output: emit-asm
//@ compile-flags: --target avr-none -C target-cpu=atmega328p
//@ needs-llvm-components: avr
//@ revisions: avr avrtiny
//@[avr] compile-flags: --target avr-none -C target-cpu=atmega328p
//@[avr] needs-llvm-components: avr
//@[avrtiny] compile-flags: --target avr-none -C target-cpu=attiny104
//@[avrtiny] needs-llvm-components: avr
// ignore-tidy-linelength

#![crate_type = "rlib"]
#![feature(no_core, asm_experimental_arch)]
Expand All @@ -25,14 +29,16 @@ pub unsafe fn sreg_is_not_clobbered_if_preserve_flags_is_used() {
}

// CHECK-LABEL: @clobber_abi
// CHECK: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31},~{sreg}"()
// avr: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31},~{sreg}"()
// avrtiny: asm sideeffect "", "={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}"()
// avr: asm sideeffect "", "={r18},={r19},={r20},={r21},={r22},={r23},={r24},={r25},={r26},={r27},={r30},={r31}"()
// avrtiny: asm sideeffect "", "={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));
Expand Down
56 changes: 56 additions & 0 deletions tests/ui/asm/avr/bad-reg.avr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
error: invalid register `Y`: the frame pointer cannot be used as an operand for inline asm
--> $DIR/bad-reg.rs:20:18
|
LL | asm!("", out("Y") _);
| ^^^^^^^^^^

error: invalid register `YL`: the frame pointer cannot be used as an operand for inline asm
--> $DIR/bad-reg.rs:22:18
|
LL | asm!("", out("YL") _);
| ^^^^^^^^^^^

error: invalid register `YH`: the frame pointer cannot be used as an operand for inline asm
--> $DIR/bad-reg.rs:24:18
|
LL | asm!("", out("YH") _);
| ^^^^^^^^^^^

error: invalid register `SP`: the stack pointer cannot be used as an operand for inline asm
--> $DIR/bad-reg.rs:26:18
|
LL | asm!("", out("SP") _);
| ^^^^^^^^^^^

error: invalid register `SPL`: the stack pointer cannot be used as an operand for inline asm
--> $DIR/bad-reg.rs:28:18
|
LL | asm!("", out("SPL") _);
| ^^^^^^^^^^^^

error: invalid register `SPH`: the stack pointer cannot be used as an operand for inline asm
--> $DIR/bad-reg.rs:30:18
|
LL | asm!("", out("SPH") _);
| ^^^^^^^^^^^^

error: invalid register `r0`: LLVM reserves r0 (scratch register) and r1 (zero register)
--> $DIR/bad-reg.rs:32:18
|
LL | asm!("", out("r0") _);
| ^^^^^^^^^^^

error: invalid register `r1`: LLVM reserves r0 (scratch register) and r1 (zero register)
--> $DIR/bad-reg.rs:34:18
|
LL | asm!("", out("r1") _);
| ^^^^^^^^^^^

error: invalid register `r1r0`: LLVM reserves r0 (scratch register) and r1 (zero register)
--> $DIR/bad-reg.rs:36:18
|
LL | asm!("", out("r1r0") _);
| ^^^^^^^^^^^^^

error: aborting due to 9 previous errors

Loading
Loading