Skip to content

Commit

Permalink
Use far-ranged jumps for DH_TRAMPOLINE
Browse files Browse the repository at this point in the history
Fixes japaric/cortex-m-rtfm#42

Note that this will make all generated crates that target an armv6
device fail to compile unless they add a build script enabling the added
`cfg`.
  • Loading branch information
jonas-schievink authored and therealprof committed Sep 11, 2017
1 parent 4028603 commit 39c64c0
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn device(
});
}

::generate::interrupt(target, &d.peripherals, items);
::generate::interrupt(d, target, &d.peripherals, items);

const CORE_PERIPHERALS: &[&str] = &[
"CPUID",
Expand Down Expand Up @@ -173,6 +173,7 @@ pub fn device(

/// Generates code for `src/interrupt.rs`
pub fn interrupt(
device: &Device,
target: &Target,
peripherals: &[Peripheral],
items: &mut Vec<Tokens>,
Expand Down Expand Up @@ -241,21 +242,48 @@ pub fn interrupt(
let n = util::unsuffixed(u64(pos));
match *target {
Target::CortexM => {
mod_items.push(quote! {
#[cfg(all(target_arch = "arm", feature = "rt"))]
global_asm!("
.thumb_func
DH_TRAMPOLINE:
b DEFAULT_HANDLER
");
let is_armv6 = match device.cpu {
Some(ref cpu) if cpu.name.starts_with("CM0") => true,
_ => false,
};

/// Hack to compile on x86
#[cfg(all(target_arch = "x86_64", feature = "rt"))]
global_asm!("
DH_TRAMPOLINE:
jmp DEFAULT_HANDLER
");
if is_armv6 {
// Cortex-M0(+) are ARMv6 and don't have `b.w` (branch with 16 MB range). This
// can cause linker errors when the handler is too far away. Instead of a small
// inline assembly shim, we generate a function for those targets and let the
// compiler do the work (sacrificing a few bytes of code).
mod_items.push(quote! {
#[cfg(feature = "rt")]
extern "C" {
fn DEFAULT_HANDLER();
}

#[cfg(feature = "rt")]
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn DH_TRAMPOLINE() {
DEFAULT_HANDLER();
}
});
} else {
mod_items.push(quote! {
#[cfg(all(target_arch = "arm", feature = "rt"))]
global_asm!("
.thumb_func
DH_TRAMPOLINE:
b DEFAULT_HANDLER
");

/// Hack to compile on x86
#[cfg(all(target_arch = "x86_64", feature = "rt"))]
global_asm!("
DH_TRAMPOLINE:
jmp DEFAULT_HANDLER
");
})
}

mod_items.push(quote! {
#[cfg(feature = "rt")]
global_asm!(#aliases);

Expand Down

0 comments on commit 39c64c0

Please sign in to comment.