Skip to content

Commit

Permalink
[AVR] Add AVR platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster authored and dylanmckay committed Jun 8, 2019
1 parent d71ae5e commit 043550d
Show file tree
Hide file tree
Showing 21 changed files with 191 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
# not built by default and the experimental Rust compilation targets that depend
# on them will not work unless the user opts in to building them. By default the
# `WebAssembly` and `RISCV` targets are enabled when compiling LLVM from scratch.
#experimental-targets = "WebAssembly;RISCV"
#experimental-targets = "AVR;WebAssembly;RISCV"

# Cap the number of parallel linker invocations when compiling LLVM.
# This can be useful when building LLVM with debug info, which significantly
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ impl Config {
set(&mut config.llvm_link_shared, llvm.link_shared);
config.llvm_targets = llvm.targets.clone();
config.llvm_experimental_targets = llvm.experimental_targets.clone()
.unwrap_or_else(|| "WebAssembly;RISCV".to_string());
.unwrap_or_else(|| "AVR;WebAssembly;RISCV".to_string());
config.llvm_link_jobs = llvm.link_jobs;
config.llvm_version_suffix = llvm.version_suffix.clone();
config.llvm_clang_cl = llvm.clang_cl.clone();
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ impl_stable_hash_for!(enum ::rustc_target::spec::abi::Abi {
Msp430Interrupt,
X86Interrupt,
AmdGpuKernel,
AvrInterrupt,
AvrNonBlockingInterrupt,
Rust,
C,
System,
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,8 @@ where
Msp430Interrupt => Conv::Msp430Intr,
X86Interrupt => Conv::X86Intr,
AmdGpuKernel => Conv::AmdGpuKernel,
AvrInterrupt => Conv::AvrInterrupt,
AvrNonBlockingInterrupt => Conv::AvrNonBlockingInterrupt,

// These API constants ought to be more specific...
Cdecl => Conv::C,
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_codegen_llvm/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ impl<'tcx> FnTypeLlvmExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
match self.conv {
Conv::C => llvm::CCallConv,
Conv::AmdGpuKernel => llvm::AmdGpuKernel,
Conv::AvrInterrupt => llvm::AvrInterrupt,
Conv::AvrNonBlockingInterrupt => llvm::AvrNonBlockingInterrupt,
Conv::ArmAapcs => llvm::ArmAapcsCallConv,
Conv::Msp430Intr => llvm::Msp430Intr,
Conv::PtxKernel => llvm::PtxKernel,
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_codegen_llvm/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub enum CallConv {
X86_64_Win64 = 79,
X86_VectorCall = 80,
X86_Intr = 83,
AvrNonBlockingInterrupt = 84,
AvrInterrupt = 85,
AmdGpuKernel = 91,
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn main() {
let is_crossed = target != host;

let mut optional_components =
vec!["x86", "arm", "aarch64", "amdgpu", "mips", "powerpc",
vec!["x86", "arm", "aarch64", "amdgpu", "avr", "mips", "powerpc",
"systemz", "jsbackend", "webassembly", "msp430", "sparc", "nvptx"];

let mut version_cmd = Command::new(&llvm_config);
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ pub fn initialize_available_targets() {
LLVMInitializeARMTargetMC,
LLVMInitializeARMAsmPrinter,
LLVMInitializeARMAsmParser);
init_target!(llvm_component = "avr",
LLVMInitializeAVRTargetInfo,
LLVMInitializeAVRTarget,
LLVMInitializeAVRTargetMC,
LLVMInitializeAVRAsmPrinter,
LLVMInitializeAVRAsmParser);
init_target!(llvm_component = "aarch64",
LLVMInitializeAArch64TargetInfo,
LLVMInitializeAArch64Target,
Expand Down
43 changes: 43 additions & 0 deletions src/librustc_target/abi/call/avr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT

This comment has been minimized.

Copy link
@jplatte

jplatte Dec 13, 2019

These comments were removed from rust a while ago, right? I don't see them in any of the files that this commit modifies, only in the ones that are newly created.

This comment has been minimized.

Copy link
@shepmaster

shepmaster Dec 13, 2019

Author Member

Totally believable. This file has existed in one form or another for many years, with only minimal changes to keep it compiling.

// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(non_upper_case_globals)]

use crate::abi::call::{FnType, ArgType};

fn classify_ret_ty<Ty>(ret: &mut ArgType<'_, Ty>) {
if ret.layout.is_aggregate() {
ret.make_indirect();
} else {
ret.extend_integer_width_to(8); // Is 8 correct?
}
}

fn classify_arg_ty<Ty>(arg: &mut ArgType<'_, Ty>) {
if arg.layout.is_aggregate() {
arg.make_indirect();
} else {
arg.extend_integer_width_to(8);
}
}

pub fn compute_abi_info<Ty>(fty: &mut FnType<'_, Ty>) {
if !fty.ret.is_ignore() {
classify_ret_ty(&mut fty.ret);
}

for arg in &mut fty.args {
if arg.is_ignore() {
continue;
}

classify_arg_ty(arg);
}
}
4 changes: 4 additions & 0 deletions src/librustc_target/abi/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod aarch64;
mod amdgpu;
mod arm;
mod asmjs;
mod avr;
mod hexagon;
mod mips;
mod mips64;
Expand Down Expand Up @@ -516,6 +517,8 @@ pub enum Conv {
X86_64Win64,

AmdGpuKernel,
AvrInterrupt,
AvrNonBlockingInterrupt,
}

/// Metadata describing how the arguments to a native function
Expand Down Expand Up @@ -573,6 +576,7 @@ impl<'a, Ty> FnType<'a, Ty> {
wasm32::compute_abi_info(self)
}
}
"avr" => avr::compute_abi_info(self),
"msp430" => msp430::compute_abi_info(self),
"sparc" => sparc::compute_abi_info(cx, self),
"sparc64" => sparc64::compute_abi_info(cx, self),
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_target/spec/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub enum Abi {
Msp430Interrupt,
X86Interrupt,
AmdGpuKernel,
AvrInterrupt,
AvrNonBlockingInterrupt,

// Multiplatform / generic ABIs
Rust,
Expand Down Expand Up @@ -55,6 +57,9 @@ const AbiDatas: &[AbiData] = &[
AbiData {abi: Abi::Msp430Interrupt, name: "msp430-interrupt", generic: false },
AbiData {abi: Abi::X86Interrupt, name: "x86-interrupt", generic: false },
AbiData {abi: Abi::AmdGpuKernel, name: "amdgpu-kernel", generic: false },
AbiData {abi: Abi::AvrInterrupt, name: "avr-interrupt", generic: false },
AbiData {abi: Abi::AvrNonBlockingInterrupt, name: "avr-non-blocking-interrupt",
generic: false },

// Cross-platform ABIs
AbiData {abi: Abi::Rust, name: "Rust", generic: true },
Expand Down
27 changes: 27 additions & 0 deletions src/librustc_target/spec/avr_unknown_unknown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::spec::{LinkerFlavor, Target, TargetResult};

pub fn target() -> TargetResult {
Ok(Target {
llvm_target: "avr-unknown-unknown".to_string(),
target_endian: "little".to_string(),
target_pointer_width: "16".to_string(),
data_layout: "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8".to_string(),
arch: "avr".to_string(),
linker_flavor: LinkerFlavor::Gcc,
target_os: "unknown".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
target_c_int_width: 16.to_string(),
options: super::none_base::opts(),
})
}
5 changes: 4 additions & 1 deletion src/librustc_target/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod haiku_base;
mod hermit_base;
mod linux_base;
mod linux_musl_base;
mod none_base;
mod openbsd_base;
mod netbsd_base;
mod solaris_base;
Expand Down Expand Up @@ -411,6 +412,8 @@ supported_targets! {
("aarch64-fuchsia", aarch64_fuchsia),
("x86_64-fuchsia", x86_64_fuchsia),

("avr-unknown-unknown", avr_unknown_unknown),

("x86_64-unknown-l4re-uclibc", x86_64_unknown_l4re_uclibc),

("x86_64-unknown-redox", x86_64_unknown_redox),
Expand Down Expand Up @@ -496,7 +499,7 @@ pub struct Target {
/// Vendor name to use for conditional compilation.
pub target_vendor: String,
/// Architecture to use for ABI considerations. Valid options: "x86",
/// "x86_64", "arm", "aarch64", "mips", "powerpc", and "powerpc64".
/// "x86_64", "arm", "aarch64", "avr", "mips", "powerpc", and "powerpc64".
pub arch: String,
/// [Data layout](http://llvm.org/docs/LangRef.html#data-layout) to pass to LLVM.
pub data_layout: String,
Expand Down
37 changes: 37 additions & 0 deletions src/librustc_target/spec/none_base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::default::Default;
use crate::spec::{LinkerFlavor, LinkArgs, TargetOptions};

pub fn opts() -> TargetOptions {
let mut args = LinkArgs::new();

args.insert(LinkerFlavor::Gcc, vec![
// We want to be able to strip as much executable code as possible
// from the linker command line, and this flag indicates to the
// linker that it can avoid linking in dynamic libraries that don't
// actually satisfy any symbols up to that point (as with many other
// resolutions the linker does). This option only applies to all
// following libraries so we're sure to pass it as one of the first
// arguments.
"-Wl,--as-needed".to_string(),
]);

TargetOptions {
dynamic_linking: false,
executables: true,
linker_is_gnu: true,
has_rpath: false,
pre_link_args: args,
position_independent_executables: true,
.. Default::default()
}
}
6 changes: 6 additions & 0 deletions src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ pub mod consts {
/// - x86_64
/// - arm
/// - aarch64
/// - avr
/// - mips
/// - mips64
/// - powerpc
Expand Down Expand Up @@ -930,6 +931,11 @@ mod arch {
pub const ARCH: &str = "aarch64";
}

#[cfg(target_arch = "avr")]
mod arch {
pub const ARCH: &'static str = "avr";
}

#[cfg(target_arch = "mips")]
mod arch {
pub const ARCH: &str = "mips";
Expand Down
8 changes: 8 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ declare_features! (
// Allows `extern "x86-interrupt" fn()`.
(active, abi_x86_interrupt, "1.17.0", Some(40180), None),

// `extern "avr-interrupt" fn()`
(active, abi_avr_interrupt, "1.18.0", Some(000), None),

// Allows module-level inline assembly by way of `global_asm!()`.
(active, global_asm, "1.18.0", Some(35119), None),

Expand Down Expand Up @@ -1838,6 +1841,11 @@ impl<'a> PostExpansionVisitor<'a> {
Abi::X86Interrupt => {
gate_feature_post!(&self, abi_x86_interrupt, span,
"x86-interrupt ABI is experimental and subject to change");
}
Abi::AvrInterrupt | Abi::AvrNonBlockingInterrupt => {
gate_feature_post!(&self, abi_avr_interrupt, span,
"avr-interrupt and avr-non-blocking-interrupt ABIs are \
experimental and subject to change");
},
Abi::AmdGpuKernel => {
gate_feature_post!(&self, abi_amdgpu_kernel, span,
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ symbols! {
abi_unadjusted,
abi_vectorcall,
abi_x86_interrupt,
abi_avr_interrupt,
aborts,
advanced_slice_patterns,
adx_target_feature,
Expand Down
7 changes: 7 additions & 0 deletions src/rustllvm/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ void LLVMRustPassManagerBuilderPopulateThinLTOPassManager(
#define SUBTARGET_AARCH64
#endif

#ifdef LLVM_COMPONENT_AVR
#define SUBTARGET_AVR SUBTARGET(AVR)
#else
#define SUBTARGET_AVR
#endif

#ifdef LLVM_COMPONENT_MIPS
#define SUBTARGET_MIPS SUBTARGET(Mips)
#else
Expand Down Expand Up @@ -163,6 +169,7 @@ void LLVMRustPassManagerBuilderPopulateThinLTOPassManager(
SUBTARGET_X86 \
SUBTARGET_ARM \
SUBTARGET_AARCH64 \
SUBTARGET_AVR \
SUBTARGET_MIPS \
SUBTARGET_PPC \
SUBTARGET_SYSTEMZ \
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/feature-gates/feature-gate-abi-avr-interrupt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test that the AVR interrupt ABI cannot be used when avr_interrupt
// feature gate is not used.

extern "avr-interrupt" fn foo() {}
//~^ ERROR avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change

fn main() {
foo();
}
11 changes: 11 additions & 0 deletions src/test/ui/feature-gates/feature-gate-abi-avr-interrupt.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0658]: avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change
--> $DIR/feature-gate-abi-avr-interrupt.rs:14:1
|
LL | extern "avr-interrupt" fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(abi_avr_interrupt)] to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
2 changes: 2 additions & 0 deletions src/tools/compiletest/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const OS_TABLE: &'static [(&'static str, &'static str)] = &[
("mingw32", "windows"),
("none", "none"),
("netbsd", "netbsd"),
("none", "none"),

This comment has been minimized.

Copy link
@jplatte

jplatte Dec 13, 2019

This entry already exists two lines above.

This comment has been minimized.

Copy link
@shepmaster

shepmaster Dec 13, 2019

Author Member

It does indeed (and probably didn't during the original implementation).

("openbsd", "openbsd"),
("redox", "redox"),
("sgx", "sgx"),
Expand All @@ -42,6 +43,7 @@ const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
("armv7", "arm"),
("armv7s", "arm"),
("asmjs", "asmjs"),
("avr", "avr"),
("hexagon", "hexagon"),
("i386", "x86"),
("i586", "x86"),
Expand Down

0 comments on commit 043550d

Please sign in to comment.