Skip to content

Commit 2f4b347

Browse files
shepmasterdylanmckay
authored andcommitted
[AVR] Add AVR platform support
1 parent 46f5aa9 commit 2f4b347

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
@@ -123,7 +123,7 @@ impl Step for Llvm {
123123

124124
let llvm_exp_targets = match builder.config.llvm_experimental_targets {
125125
Some(ref s) => s,
126-
None => "",
126+
None => "AVR",
127127
};
128128

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

src/librustc/ty/layout.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,8 @@ where
25102510
Msp430Interrupt => Conv::Msp430Intr,
25112511
X86Interrupt => Conv::X86Intr,
25122512
AmdGpuKernel => Conv::AmdGpuKernel,
2513+
AvrInterrupt => Conv::AvrInterrupt,
2514+
AvrNonBlockingInterrupt => Conv::AvrNonBlockingInterrupt,
25132515

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

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
@@ -377,6 +377,8 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
377377
match self.conv {
378378
Conv::C | Conv::Rust => llvm::CCallConv,
379379
Conv::AmdGpuKernel => llvm::AmdGpuKernel,
380+
Conv::AvrInterrupt => llvm::AvrInterrupt,
381+
Conv::AvrNonBlockingInterrupt => llvm::AvrNonBlockingInterrupt,
380382
Conv::ArmAapcs => llvm::ArmAapcsCallConv,
381383
Conv::Msp430Intr => llvm::Msp430Intr,
382384
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
@@ -342,6 +342,9 @@ declare_features! (
342342
/// Allows `extern "msp430-interrupt" fn()`.
343343
(active, abi_msp430_interrupt, "1.16.0", Some(38487), None),
344344

345+
/// Allows `extern "avr-interrupt" fn()` and `extern "avr-non-blocking-interrupt" fn()`.
346+
(active, abi_avr_interrupt, "1.41.0", None, None),
347+
345348
/// Allows declarative macros 2.0 (`macro`).
346349
(active, decl_macro, "1.17.0", Some(39412), None),
347350

src/librustc_llvm/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ fn main() {
6868
"arm",
6969
"aarch64",
7070
"amdgpu",
71+
"avr",
7172
"mips",
7273
"powerpc",
7374
"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_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
aborts,
124125
address,
125126
add_with_overflow,

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;
@@ -522,6 +523,8 @@ pub enum Conv {
522523
X86_64Win64,
523524

524525
AmdGpuKernel,
526+
AvrInterrupt,
527+
AvrNonBlockingInterrupt,
525528
}
526529

527530
/// Metadata describing how the arguments to a native function
@@ -575,6 +578,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
575578
"aarch64" => aarch64::compute_abi_info(cx, self),
576579
"amdgpu" => amdgpu::compute_abi_info(cx, self),
577580
"arm" => arm::compute_abi_info(cx, self),
581+
"avr" => avr::compute_abi_info(self),
578582
"mips" => mips::compute_abi_info(cx, self),
579583
"mips64" => mips64::compute_abi_info(cx, self),
580584
"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
@@ -36,6 +36,8 @@ pub enum Abi {
3636
X86Interrupt,
3737
AmdGpuKernel,
3838
EfiApi,
39+
AvrInterrupt,
40+
AvrNonBlockingInterrupt,
3941

4042
// Multiplatform / generic ABIs
4143
Rust,
@@ -74,6 +76,12 @@ const AbiDatas: &[AbiData] = &[
7476
AbiData { abi: Abi::X86Interrupt, name: "x86-interrupt", generic: false },
7577
AbiData { abi: Abi::AmdGpuKernel, name: "amdgpu-kernel", generic: false },
7678
AbiData { abi: Abi::EfiApi, name: "efiapi", generic: false },
79+
AbiData { abi: Abi::AvrInterrupt, name: "avr-interrupt", generic: false },
80+
AbiData {
81+
abi: Abi::AvrNonBlockingInterrupt,
82+
name: "avr-non-blocking-interrupt",
83+
generic: false,
84+
},
7785
// Cross-platform ABIs
7886
AbiData { abi: Abi::Rust, name: "Rust", generic: true },
7987
AbiData { abi: Abi::C, name: "C", 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
@@ -61,6 +61,7 @@ mod linux_base;
6161
mod linux_kernel_base;
6262
mod linux_musl_base;
6363
mod netbsd_base;
64+
mod none_base;
6465
mod openbsd_base;
6566
mod redox_base;
6667
mod riscv_base;
@@ -423,6 +424,8 @@ supported_targets! {
423424
("aarch64-fuchsia", aarch64_fuchsia),
424425
("x86_64-fuchsia", x86_64_fuchsia),
425426

427+
("avr-unknown-unknown", avr_unknown_unknown),
428+
426429
("x86_64-unknown-l4re-uclibc", x86_64_unknown_l4re_uclibc),
427430

428431
("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: true,
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
@@ -205,6 +205,12 @@ void LLVMRustAddLastExtensionPasses(
205205
#define SUBTARGET_AARCH64
206206
#endif
207207

208+
#ifdef LLVM_COMPONENT_AVR
209+
#define SUBTARGET_AVR SUBTARGET(AVR)
210+
#else
211+
#define SUBTARGET_AVR
212+
#endif
213+
208214
#ifdef LLVM_COMPONENT_MIPS
209215
#define SUBTARGET_MIPS SUBTARGET(Mips)
210216
#else
@@ -251,6 +257,7 @@ void LLVMRustAddLastExtensionPasses(
251257
SUBTARGET_X86 \
252258
SUBTARGET_ARM \
253259
SUBTARGET_AARCH64 \
260+
SUBTARGET_AVR \
254261
SUBTARGET_MIPS \
255262
SUBTARGET_PPC \
256263
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
@@ -46,6 +46,7 @@ const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
4646
("armv7", "arm"),
4747
("armv7s", "arm"),
4848
("asmjs", "asmjs"),
49+
("avr", "avr"),
4950
("hexagon", "hexagon"),
5051
("i386", "x86"),
5152
("i586", "x86"),

0 commit comments

Comments
 (0)