diff options
Diffstat (limited to 'macros/src')
-rw-r--r-- | macros/src/codegen.rs | 2 | ||||
-rw-r--r-- | macros/src/codegen/dispatchers.rs | 7 | ||||
-rw-r--r-- | macros/src/codegen/hardware_tasks.rs | 8 | ||||
-rw-r--r-- | macros/src/codegen/idle.rs | 13 | ||||
-rw-r--r-- | macros/src/codegen/init.rs | 4 | ||||
-rw-r--r-- | macros/src/codegen/locals.rs | 5 | ||||
-rw-r--r-- | macros/src/codegen/resources.rs | 17 | ||||
-rw-r--r-- | macros/src/codegen/schedule.rs | 4 | ||||
-rw-r--r-- | macros/src/codegen/software_tasks.rs | 24 | ||||
-rw-r--r-- | macros/src/codegen/spawn.rs | 4 | ||||
-rw-r--r-- | macros/src/codegen/timer_queue.rs | 6 | ||||
-rw-r--r-- | macros/src/codegen/util.rs | 39 |
12 files changed, 111 insertions, 22 deletions
diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 8a548323..8ac06d53 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -68,8 +68,10 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { let cfg_core = util::cfg_core(core, app.args.cores); let main = util::suffixed("main", core); + let section = util::link_section("text", core); mains.push(quote!( #[no_mangle] + #section #cfg_core unsafe extern "C" fn #main() -> ! { #(#assertion_stmts)* diff --git a/macros/src/codegen/dispatchers.rs b/macros/src/codegen/dispatchers.rs index 988e3c84..9a9cb102 100644 --- a/macros/src/codegen/dispatchers.rs +++ b/macros/src/codegen/dispatchers.rs @@ -46,13 +46,14 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream let n = util::capacity_typenum(channel.capacity, true); let rq = util::rq_ident(receiver, level, sender); - let (rq_attr, rq_ty, rq_expr) = if sender == receiver { + let (rq_attr, rq_ty, rq_expr, section) = if sender == receiver { ( cfg_sender.clone(), quote!(rtfm::export::SCRQ<#t, #n>), quote!(rtfm::export::Queue(unsafe { rtfm::export::iQueue::u8_sc() })), + util::link_section("bss", sender), ) } else { let shared = if cfg!(feature = "heterogeneous") { @@ -65,6 +66,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream shared, quote!(rtfm::export::MCRQ<#t, #n>), quote!(rtfm::export::Queue(rtfm::export::iQueue::u8())), + None, ) }; @@ -77,6 +79,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream items.push(quote!( #[doc = #doc] #rq_attr + #section static mut #rq: #rq_ty = #rq_expr; )); @@ -162,12 +165,14 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream receiver, level ); let cfg_receiver = util::cfg_core(receiver, app.args.cores); + let section = util::link_section("text", receiver); let interrupt = util::suffixed(&interrupts[&level].to_string(), receiver); items.push(quote!( #[allow(non_snake_case)] #[doc = #doc] #[no_mangle] #cfg_receiver + #section unsafe fn #interrupt() { /// The priority of this interrupt handler const PRIORITY: u8 = #level; diff --git a/macros/src/codegen/hardware_tasks.rs b/macros/src/codegen/hardware_tasks.rs index a7af510a..cf92e078 100644 --- a/macros/src/codegen/hardware_tasks.rs +++ b/macros/src/codegen/hardware_tasks.rs @@ -56,9 +56,11 @@ pub fn codegen( }; let priority = task.args.priority; + let section = util::link_section("text", core); const_app.push(quote!( #[allow(non_snake_case)] #[no_mangle] + #section #cfg_core unsafe fn #symbol() { const PRIORITY: u8 = #priority; @@ -101,7 +103,8 @@ pub fn codegen( // `${task}Locals` let mut locals_pat = None; if !task.locals.is_empty() { - let (struct_, pat) = locals::codegen(Context::HardwareTask(name), &task.locals, app); + let (struct_, pat) = + locals::codegen(Context::HardwareTask(name), &task.locals, core, app); root.push(struct_); locals_pat = Some(pat); @@ -110,9 +113,12 @@ pub fn codegen( let attrs = &task.attrs; let context = &task.context; let stmts = &task.stmts; + let section = util::link_section("text", core); + // XXX shouldn't this have a cfg_core? user_tasks.push(quote!( #(#attrs)* #[allow(non_snake_case)] + #section fn #name(#(#locals_pat,)* #context: #name::Context) { use rtfm::Mutex as _; diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index 7af01c91..d6560761 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -44,7 +44,7 @@ pub fn codegen( let name = &idle.name; if !idle.locals.is_empty() { - let (locals, pat) = locals::codegen(Context::Idle(core), &idle.locals, app); + let (locals, pat) = locals::codegen(Context::Idle(core), &idle.locals, core, app); locals_new = Some(quote!(#name::Locals::new())); locals_pat = Some(pat); @@ -57,10 +57,12 @@ pub fn codegen( let attrs = &idle.attrs; let context = &idle.context; let stmts = &idle.stmts; + let section = util::link_section("text", core); let user_idle = Some(quote!( - #cfg_core #(#attrs)* #[allow(non_snake_case)] + #cfg_core + #section fn #name(#(#locals_pat,)* #context: #name::Context) -> ! { use rtfm::Mutex as _; @@ -73,12 +75,7 @@ pub fn codegen( #name::Context::new(&rtfm::export::Priority::new(0)) )); - ( - const_app, - root_idle, - user_idle, - call_idle, - ) + (const_app, root_idle, user_idle, call_idle) } else { ( None, diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 271be94c..878c633e 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -72,7 +72,7 @@ pub fn codegen( let mut locals_pat = None; let mut locals_new = None; if !init.locals.is_empty() { - let (struct_, pat) = locals::codegen(Context::Init(core), &init.locals, app); + let (struct_, pat) = locals::codegen(Context::Init(core), &init.locals, core, app); locals_new = Some(quote!(#name::Locals::new())); locals_pat = Some(pat); @@ -82,10 +82,12 @@ pub fn codegen( let context = &init.context; let attrs = &init.attrs; let stmts = &init.stmts; + let section = util::link_section("text", core); let user_init = Some(quote!( #(#attrs)* #cfg_core #[allow(non_snake_case)] + #section fn #name(#(#locals_pat,)* #context: #name::Context) #ret { #(#stmts)* } diff --git a/macros/src/codegen/locals.rs b/macros/src/codegen/locals.rs index 96635637..799ef7a0 100644 --- a/macros/src/codegen/locals.rs +++ b/macros/src/codegen/locals.rs @@ -2,7 +2,7 @@ use proc_macro2::TokenStream as TokenStream2; use quote::quote; use rtfm_syntax::{ ast::{App, Local}, - Context, Map, + Context, Core, Map, }; use crate::codegen::util; @@ -10,6 +10,7 @@ use crate::codegen::util; pub fn codegen( ctxt: Context, locals: &Map<Local>, + core: Core, app: &App, ) -> ( // locals @@ -41,6 +42,7 @@ pub fn codegen( let cfgs = &local.cfgs; has_cfgs |= !cfgs.is_empty(); + let section = util::link_section("data", core); let expr = &local.expr; let ty = &local.ty; fields.push(quote!( @@ -49,6 +51,7 @@ pub fn codegen( )); items.push(quote!( #(#cfgs)* + #section static mut #name: #ty = #expr )); values.push(quote!( diff --git a/macros/src/codegen/resources.rs b/macros/src/codegen/resources.rs index 2425681b..1161a7a5 100644 --- a/macros/src/codegen/resources.rs +++ b/macros/src/codegen/resources.rs @@ -26,20 +26,24 @@ pub fn codegen( let ty = &res.ty; { - let loc_attr = match loc { + let (loc_attr, section) = match loc { Location::Owned { core, cross_initialized: false, - } => util::cfg_core(*core, app.args.cores), + } => ( + util::cfg_core(*core, app.args.cores), + util::link_section("data", *core), + ), // shared `static`s and cross-initialized resources need to be in `.shared` memory - _ => { + _ => ( if cfg!(feature = "heterogeneous") { Some(quote!(#[rtfm::export::shared])) } else { None - } - } + }, + None, + ), }; let (ty, expr) = if let Some(expr) = expr { @@ -53,9 +57,10 @@ pub fn codegen( let attrs = &res.attrs; const_app.push(quote!( - #loc_attr #(#attrs)* #(#cfgs)* + #loc_attr + #section static mut #name: #ty = #expr; )); } diff --git a/macros/src/codegen/schedule.rs b/macros/src/codegen/schedule.rs index 57f01a2c..8cf60985 100644 --- a/macros/src/codegen/schedule.rs +++ b/macros/src/codegen/schedule.rs @@ -35,8 +35,10 @@ pub fn codegen(app: &App, extra: &Extra) -> Vec<TokenStream2> { let body = schedule_body::codegen(scheduler, &name, app); + let section = util::link_section("text", sender); methods.push(quote!( #(#cfgs)* + #section fn #name(&self, instant: #instant #(,#args)*) -> Result<(), #ty> { #body } @@ -50,9 +52,11 @@ pub fn codegen(app: &App, extra: &Extra) -> Vec<TokenStream2> { let body = schedule_body::codegen(scheduler, &name, app); + let section = util::link_section("text", sender); items.push(quote!( #cfg_sender #(#cfgs)* + #section unsafe fn #schedule( priority: &rtfm::export::Priority, instant: #instant diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 383a5d82..2960faf9 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -43,13 +43,21 @@ pub fn codegen( let cfg_sender = util::cfg_core(sender, app.args.cores); let fq = util::fq_ident(name, sender); - let (loc, fq_ty, fq_expr) = if receiver == sender { + let (loc, fq_ty, fq_expr, bss, mk_uninit): ( + _, + _, + _, + _, + Box<dyn Fn() -> Option<_>>, + ) = if receiver == sender { ( cfg_sender.clone(), quote!(rtfm::export::SCFQ<#cap_ty>), quote!(rtfm::export::Queue(unsafe { rtfm::export::iQueue::u8_sc() })), + util::link_section("bss", sender), + Box::new(|| util::link_section_uninit(Some(sender))), ) } else { let shared = if cfg!(feature = "heterogeneous") { @@ -62,6 +70,8 @@ pub fn codegen( shared, quote!(rtfm::export::MCFQ<#cap_ty>), quote!(rtfm::export::Queue(rtfm::export::iQueue::u8())), + None, + Box::new(|| util::link_section_uninit(None)), ) }; let loc = &loc; @@ -70,6 +80,7 @@ pub fn codegen( /// Queue version of a free-list that keeps track of empty slots in /// the following buffers #loc + #bss static mut #fq: #fq_ty = #fq_expr; )); @@ -102,8 +113,10 @@ pub fn codegen( let m = extra.monotonic(); let instants = util::instants_ident(name, sender); + let uninit = mk_uninit(); const_app.push(quote!( #loc + #uninit /// Buffer that holds the instants associated to the inputs of a task static mut #instants: [core::mem::MaybeUninit<<#m as rtfm::Monotonic>::Instant>; #cap_lit] = @@ -111,9 +124,11 @@ pub fn codegen( )); } + let uninit = mk_uninit(); let inputs = util::inputs_ident(name, sender); const_app.push(quote!( #loc + #uninit /// Buffer that holds the inputs of a task static mut #inputs: [core::mem::MaybeUninit<#input_ty>; #cap_lit] = [#(#elems,)*]; @@ -140,13 +155,15 @@ pub fn codegen( // `${task}Locals` let mut locals_pat = None; if !task.locals.is_empty() { - let (struct_, pat) = locals::codegen(Context::SoftwareTask(name), &task.locals, app); + let (struct_, pat) = + locals::codegen(Context::SoftwareTask(name), &task.locals, receiver, app); locals_pat = Some(pat); root.push(struct_); } let cfg_receiver = util::cfg_core(receiver, app.args.cores); + let section = util::link_section("text", receiver); let context = &task.context; let attrs = &task.attrs; let cfgs = &task.cfgs; @@ -154,8 +171,9 @@ pub fn codegen( user_tasks.push(quote!( #(#attrs)* #(#cfgs)* - #cfg_receiver #[allow(non_snake_case)] + #cfg_receiver + #section fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) { use rtfm::Mutex as _; diff --git a/macros/src/codegen/spawn.rs b/macros/src/codegen/spawn.rs index 1539e277..c63c410b 100644 --- a/macros/src/codegen/spawn.rs +++ b/macros/src/codegen/spawn.rs @@ -42,8 +42,10 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream None }; + let section = util::link_section("text", sender); methods.push(quote!( #(#cfgs)* + #section fn #name(&self #(,#args)*) -> Result<(), #ty> { #let_instant #body @@ -66,9 +68,11 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream let body = spawn_body::codegen(spawner, &name, app, analysis, extra); + let section = util::link_section("text", sender); items.push(quote!( #cfg_sender #(#cfgs)* + #section unsafe fn #spawn( priority: &rtfm::export::Priority #instant diff --git a/macros/src/codegen/timer_queue.rs b/macros/src/codegen/timer_queue.rs index d306ed5b..3af7e432 100644 --- a/macros/src/codegen/timer_queue.rs +++ b/macros/src/codegen/timer_queue.rs @@ -48,9 +48,11 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream let n = util::capacity_typenum(timer_queue.capacity, false); let tq_ty = quote!(rtfm::export::TimerQueue<#m, #t, #n>); + let section = util::link_section("bss", sender); items.push(quote!( #cfg_sender #[doc = #doc] + #section static mut #tq: #tq_ty = rtfm::export::TimerQueue( rtfm::export::BinaryHeap( rtfm::export::iBinaryHeap::new() @@ -117,9 +119,11 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream let priority = timer_queue.priority; let sys_tick = util::suffixed("SysTick", sender); + let section = util::link_section("text", sender); items.push(quote!( - #cfg_sender #[no_mangle] + #cfg_sender + #section unsafe fn #sys_tick() { use rtfm::Mutex as _; diff --git a/macros/src/codegen/util.rs b/macros/src/codegen/util.rs index cd01264d..f5f96dea 100644 --- a/macros/src/codegen/util.rs +++ b/macros/src/codegen/util.rs @@ -1,3 +1,5 @@ +use core::sync::atomic::{AtomicUsize, Ordering}; + use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::quote; use rtfm_syntax::{ast::App, Context, Core}; @@ -133,6 +135,43 @@ pub fn late_resources_ident(init: &Ident) -> Ident { ) } +fn link_section_index() -> usize { + static INDEX: AtomicUsize = AtomicUsize::new(0); + + INDEX.fetch_add(1, Ordering::Relaxed) +} + +pub fn link_section(section: &str, core: Core) -> Option<TokenStream2> { + if cfg!(feature = "homogeneous") { + let section = format!(".{}_{}.rtfm{}", section, core, link_section_index()); + Some(quote!(#[link_section = #section])) + } else { + None + } +} + +// NOTE `None` means in shared memory +pub fn link_section_uninit(core: Option<Core>) -> Option<TokenStream2> { + let section = if let Some(core) = core { + let index = link_section_index(); + + if cfg!(feature = "homogeneous") { + format!(".uninit_{}.rtfm{}", core, index) + } else { + format!(".uninit.rtfm{}", index) + } + } else { + if cfg!(feature = "heterogeneous") { + // `#[shared]` attribute sets the linker section + return None; + } + + format!(".uninit.rtfm{}", link_section_index()) + }; + + Some(quote!(#[link_section = #section])) +} + /// Generates a pre-reexport identifier for the "locals" struct pub fn locals_ident(ctxt: Context, app: &App) -> Ident { let mut s = match ctxt { |