Skip to content

Commit 690bb8a

Browse files
shepmasterdylanmckay
authored andcommitted
[AVR] Add AVR platform support
1 parent 5d39f1f commit 690bb8a

File tree

21 files changed

+158
-2
lines changed

21 files changed

+158
-2
lines changed

config.toml.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
# the same format as above, but since these targets are experimental, they are
7070
# not built by default and the experimental Rust compilation targets that depend
7171
# on them will not work unless the user opts in to building them.
72-
#experimental-targets = ""
72+
#experimental-targets = "AVR"
7373

7474
# Cap the number of parallel linker invocations when compiling LLVM.
7575
# This can be useful when building LLVM with debug info, which significantly

src/bootstrap/native.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl Step for Llvm {
144144

145145
let llvm_exp_targets = match builder.config.llvm_experimental_targets {
146146
Some(ref s) => s,
147-
None => "",
147+
None => "AVR",
148148
};
149149

150150
let assertions = if builder.config.llvm_assertions { "ON" } else { "OFF" };

src/librustc_ast_passes/feature_gate.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ impl<'a> PostExpansionVisitor<'a> {
121121
"amdgpu-kernel ABI is experimental and subject to change"
122122
);
123123
}
124+
"avr-interrupt" | "avr-non-blocking-interrupt" => {
125+
gate_feature_post!(
126+
&self,
127+
abi_avr_interrupt,
128+
span,
129+
"avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change"
130+
);
131+
}
124132
"efiapi" => {
125133
gate_feature_post!(
126134
&self,

src/librustc_codegen_llvm/abi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
375375
match self.conv {
376376
Conv::C | Conv::Rust => llvm::CCallConv,
377377
Conv::AmdGpuKernel => llvm::AmdGpuKernel,
378+
Conv::AvrInterrupt => llvm::AvrInterrupt,
379+
Conv::AvrNonBlockingInterrupt => llvm::AvrNonBlockingInterrupt,
378380
Conv::ArmAapcs => llvm::ArmAapcsCallConv,
379381
Conv::Msp430Intr => llvm::Msp430Intr,
380382
Conv::PtxKernel => llvm::PtxKernel,

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub enum CallConv {
4545
X86_64_Win64 = 79,
4646
X86_VectorCall = 80,
4747
X86_Intr = 83,
48+
AvrNonBlockingInterrupt = 84,
49+
AvrInterrupt = 85,
4850
AmdGpuKernel = 91,
4951
}
5052

src/librustc_feature/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ declare_features! (
347347
/// Allows `extern "msp430-interrupt" fn()`.
348348
(active, abi_msp430_interrupt, "1.16.0", Some(38487), None),
349349

350+
/// Allows `extern "avr-interrupt" fn()` and `extern "avr-non-blocking-interrupt" fn()`.
351+
(active, abi_avr_interrupt, "1.41.0", None, None),
352+
350353
/// Allows declarative macros 2.0 (`macro`).
351354
(active, decl_macro, "1.17.0", Some(39412), None),
352355

src/librustc_llvm/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ fn main() {
7878
"arm",
7979
"aarch64",
8080
"amdgpu",
81+
"avr",
8182
"mips",
8283
"powerpc",
8384
"systemz",

src/librustc_llvm/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ pub fn initialize_available_targets() {
7676
LLVMInitializeAMDGPUAsmPrinter,
7777
LLVMInitializeAMDGPUAsmParser
7878
);
79+
init_target!(
80+
llvm_component = "avr",
81+
LLVMInitializeAVRTargetInfo,
82+
LLVMInitializeAVRTarget,
83+
LLVMInitializeAVRTargetMC,
84+
LLVMInitializeAVRAsmPrinter,
85+
LLVMInitializeAVRAsmParser
86+
);
7987
init_target!(
8088
llvm_component = "mips",
8189
LLVMInitializeMipsTargetInfo,

src/librustc_middle/ty/layout.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,8 @@ where
25292529
Msp430Interrupt => Conv::Msp430Intr,
25302530
X86Interrupt => Conv::X86Intr,
25312531
AmdGpuKernel => Conv::AmdGpuKernel,
2532+
AvrInterrupt => Conv::AvrInterrupt,
2533+
AvrNonBlockingInterrupt => Conv::AvrNonBlockingInterrupt,
25322534

25332535
// These API constants ought to be more specific...
25342536
Cdecl => Conv::C,

src/librustc_span/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ symbols! {
120120
abi_unadjusted,
121121
abi_vectorcall,
122122
abi_x86_interrupt,
123+
abi_avr_interrupt,
123124
abort,
124125
aborts,
125126
address,

src/librustc_target/abi/call/avr.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![allow(non_upper_case_globals)]
2+
3+
use crate::abi::call::{ArgAbi, FnAbi};
4+
5+
fn classify_ret_ty<Ty>(ret: &mut ArgAbi<'_, Ty>) {
6+
if ret.layout.is_aggregate() {
7+
ret.make_indirect();
8+
} else {
9+
ret.extend_integer_width_to(8); // Is 8 correct?
10+
}
11+
}
12+
13+
fn classify_arg_ty<Ty>(arg: &mut ArgAbi<'_, Ty>) {
14+
if arg.layout.is_aggregate() {
15+
arg.make_indirect();
16+
} else {
17+
arg.extend_integer_width_to(8);
18+
}
19+
}
20+
21+
pub fn compute_abi_info<Ty>(fty: &mut FnAbi<'_, Ty>) {
22+
if !fty.ret.is_ignore() {
23+
classify_ret_ty(&mut fty.ret);
24+
}
25+
26+
for arg in &mut fty.args {
27+
if arg.is_ignore() {
28+
continue;
29+
}
30+
31+
classify_arg_ty(arg);
32+
}
33+
}

src/librustc_target/abi/call/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::spec::{self, HasTargetSpec};
55
mod aarch64;
66
mod amdgpu;
77
mod arm;
8+
mod avr;
89
mod hexagon;
910
mod mips;
1011
mod mips64;
@@ -525,6 +526,8 @@ pub enum Conv {
525526
X86_64Win64,
526527

527528
AmdGpuKernel,
529+
AvrInterrupt,
530+
AvrNonBlockingInterrupt,
528531
}
529532

530533
/// Metadata describing how the arguments to a native function
@@ -580,6 +583,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
580583
"aarch64" => aarch64::compute_abi_info(cx, self),
581584
"amdgpu" => amdgpu::compute_abi_info(cx, self),
582585
"arm" => arm::compute_abi_info(cx, self),
586+
"avr" => avr::compute_abi_info(self),
583587
"mips" => mips::compute_abi_info(cx, self),
584588
"mips64" => mips64::compute_abi_info(cx, self),
585589
"powerpc" => powerpc::compute_abi_info(self),

src/librustc_target/spec/abi.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub enum Abi {
3434
X86Interrupt,
3535
AmdGpuKernel,
3636
EfiApi,
37+
AvrInterrupt,
38+
AvrNonBlockingInterrupt,
3739

3840
// Multiplatform / generic ABIs
3941
System,
@@ -73,6 +75,12 @@ const AbiDatas: &[AbiData] = &[
7375
AbiData { abi: Abi::X86Interrupt, name: "x86-interrupt", generic: false },
7476
AbiData { abi: Abi::AmdGpuKernel, name: "amdgpu-kernel", generic: false },
7577
AbiData { abi: Abi::EfiApi, name: "efiapi", generic: false },
78+
AbiData { abi: Abi::AvrInterrupt, name: "avr-interrupt", generic: false },
79+
AbiData {
80+
abi: Abi::AvrNonBlockingInterrupt,
81+
name: "avr-non-blocking-interrupt",
82+
generic: false,
83+
},
7684
// Cross-platform ABIs
7785
AbiData { abi: Abi::System, name: "system", generic: true },
7886
AbiData { abi: Abi::RustIntrinsic, name: "rust-intrinsic", generic: true },
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::spec::{LinkerFlavor, Target, TargetResult};
2+
3+
pub fn target() -> TargetResult {
4+
Ok(Target {
5+
llvm_target: "avr-unknown-unknown".to_string(),
6+
target_endian: "little".to_string(),
7+
target_pointer_width: "16".to_string(),
8+
data_layout: "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8".to_string(),
9+
arch: "avr".to_string(),
10+
linker_flavor: LinkerFlavor::Gcc,
11+
target_os: "unknown".to_string(),
12+
target_env: "".to_string(),
13+
target_vendor: "unknown".to_string(),
14+
target_c_int_width: 16.to_string(),
15+
options: super::none_base::opts(),
16+
})
17+
}

src/librustc_target/spec/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ mod linux_kernel_base;
6565
mod linux_musl_base;
6666
mod msvc_base;
6767
mod netbsd_base;
68+
mod none_base;
6869
mod openbsd_base;
6970
mod redox_base;
7071
mod riscv_base;
@@ -579,6 +580,8 @@ supported_targets! {
579580
("aarch64-fuchsia", aarch64_fuchsia),
580581
("x86_64-fuchsia", x86_64_fuchsia),
581582

583+
("avr-unknown-unknown", avr_unknown_unknown),
584+
582585
("x86_64-unknown-l4re-uclibc", x86_64_unknown_l4re_uclibc),
583586

584587
("aarch64-unknown-redox", aarch64_unknown_redox),

src/librustc_target/spec/none_base.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::spec::{LinkArgs, LinkerFlavor, TargetOptions};
2+
use std::default::Default;
3+
4+
pub fn opts() -> TargetOptions {
5+
let mut args = LinkArgs::new();
6+
7+
args.insert(
8+
LinkerFlavor::Gcc,
9+
vec![
10+
// We want to be able to strip as much executable code as possible
11+
// from the linker command line, and this flag indicates to the
12+
// linker that it can avoid linking in dynamic libraries that don't
13+
// actually satisfy any symbols up to that point (as with many other
14+
// resolutions the linker does). This option only applies to all
15+
// following libraries so we're sure to pass it as one of the first
16+
// arguments.
17+
"-Wl,--as-needed".to_string(),
18+
],
19+
);
20+
21+
TargetOptions {
22+
dynamic_linking: false,
23+
executables: true,
24+
linker_is_gnu: true,
25+
has_rpath: false,
26+
pre_link_args: args,
27+
position_independent_executables: false,
28+
..Default::default()
29+
}
30+
}

src/libstd/env.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ pub mod consts {
874874
/// - x86_64
875875
/// - arm
876876
/// - aarch64
877+
/// - avr
877878
/// - mips
878879
/// - mips64
879880
/// - powerpc
@@ -986,6 +987,11 @@ mod arch {
986987
pub const ARCH: &str = "aarch64";
987988
}
988989

990+
#[cfg(target_arch = "avr")]
991+
mod arch {
992+
pub const ARCH: &'static str = "avr";
993+
}
994+
989995
#[cfg(target_arch = "mips")]
990996
mod arch {
991997
pub const ARCH: &str = "mips";

src/rustllvm/PassWrapper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ void LLVMRustAddLastExtensionPasses(
203203
#define SUBTARGET_AARCH64
204204
#endif
205205

206+
#ifdef LLVM_COMPONENT_AVR
207+
#define SUBTARGET_AVR SUBTARGET(AVR)
208+
#else
209+
#define SUBTARGET_AVR
210+
#endif
211+
206212
#ifdef LLVM_COMPONENT_MIPS
207213
#define SUBTARGET_MIPS SUBTARGET(Mips)
208214
#else
@@ -249,6 +255,7 @@ void LLVMRustAddLastExtensionPasses(
249255
SUBTARGET_X86 \
250256
SUBTARGET_ARM \
251257
SUBTARGET_AARCH64 \
258+
SUBTARGET_AVR \
252259
SUBTARGET_MIPS \
253260
SUBTARGET_PPC \
254261
SUBTARGET_SYSTEMZ \
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Test that the AVR interrupt ABI cannot be used when avr_interrupt
2+
// feature gate is not used.
3+
4+
extern "avr-interrupt" fn foo() {}
5+
//~^ ERROR avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change
6+
7+
fn main() {
8+
foo();
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0658]: avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change
2+
--> $DIR/feature-gate-abi-avr-interrupt.rs:14:1
3+
|
4+
LL | extern "avr-interrupt" fn foo() {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add #![feature(abi_avr_interrupt)] to the crate attributes to enable
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0658`.

src/tools/compiletest/src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
4747
("armv7", "arm"),
4848
("armv7s", "arm"),
4949
("asmjs", "asmjs"),
50+
("avr", "avr"),
5051
("hexagon", "hexagon"),
5152
("i386", "x86"),
5253
("i586", "x86"),

0 commit comments

Comments
 (0)