From 81275bfa4f41e2066770087f3a33cad4227eab41 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 13 Jun 2019 23:56:59 +0200 Subject: rtfm-syntax refactor + heterogeneous multi-core support --- macros/src/codegen/idle.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 macros/src/codegen/idle.rs (limited to 'macros/src/codegen/idle.rs') diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs new file mode 100644 index 00000000..7af01c91 --- /dev/null +++ b/macros/src/codegen/idle.rs @@ -0,0 +1,92 @@ +use proc_macro2::TokenStream as TokenStream2; +use quote::quote; +use rtfm_syntax::{ast::App, Context}; + +use crate::{ + analyze::Analysis, + check::Extra, + codegen::{locals, module, resources_struct, util}, +}; + +/// Generates support code for `#[idle]` functions +pub fn codegen( + core: u8, + app: &App, + analysis: &Analysis, + extra: &Extra, +) -> ( + // const_app_idle -- the `${idle}Resources` constructor + Option, + // root_idle -- items that must be placed in the root of the crate: + // - the `${idle}Locals` struct + // - the `${idle}Resources` struct + // - the `${idle}` module, which contains types like `${idle}::Context` + Vec, + // user_idle + Option, + // call_idle + TokenStream2, +) { + if let Some(idle) = app.idles.get(&core) { + let mut needs_lt = false; + let mut const_app = None; + let mut root_idle = vec![]; + let mut locals_pat = None; + let mut locals_new = None; + + if !idle.args.resources.is_empty() { + let (item, constructor) = + resources_struct::codegen(Context::Idle(core), 0, &mut needs_lt, app, analysis); + + root_idle.push(item); + const_app = Some(constructor); + } + + let name = &idle.name; + if !idle.locals.is_empty() { + let (locals, pat) = locals::codegen(Context::Idle(core), &idle.locals, app); + + locals_new = Some(quote!(#name::Locals::new())); + locals_pat = Some(pat); + root_idle.push(locals); + } + + root_idle.push(module::codegen(Context::Idle(core), needs_lt, app, extra)); + + let cfg_core = util::cfg_core(core, app.args.cores); + let attrs = &idle.attrs; + let context = &idle.context; + let stmts = &idle.stmts; + let user_idle = Some(quote!( + #cfg_core + #(#attrs)* + #[allow(non_snake_case)] + fn #name(#(#locals_pat,)* #context: #name::Context) -> ! { + use rtfm::Mutex as _; + + #(#stmts)* + } + )); + + let call_idle = quote!(#name( + #(#locals_new,)* + #name::Context::new(&rtfm::export::Priority::new(0)) + )); + + ( + const_app, + root_idle, + user_idle, + call_idle, + ) + } else { + ( + None, + vec![], + None, + quote!(loop { + rtfm::export::wfi() + }), + ) + } +} -- cgit v1.2.3 From be92041a592f65f38cee8475b61d35e7fcee3694 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 29 Jun 2019 09:11:42 +0200 Subject: WIP --- build.rs | 5 ++++- macros/src/codegen.rs | 2 ++ macros/src/codegen/dispatchers.rs | 7 ++++++- macros/src/codegen/hardware_tasks.rs | 8 +++++++- macros/src/codegen/idle.rs | 13 +++++------- macros/src/codegen/init.rs | 4 +++- macros/src/codegen/locals.rs | 5 ++++- macros/src/codegen/resources.rs | 17 ++++++++++------ macros/src/codegen/schedule.rs | 4 ++++ macros/src/codegen/software_tasks.rs | 24 +++++++++++++++++++--- macros/src/codegen/spawn.rs | 4 ++++ macros/src/codegen/timer_queue.rs | 6 +++++- macros/src/codegen/util.rs | 39 ++++++++++++++++++++++++++++++++++++ 13 files changed, 115 insertions(+), 23 deletions(-) (limited to 'macros/src/codegen/idle.rs') diff --git a/build.rs b/build.rs index 2419b4eb..14c3d248 100644 --- a/build.rs +++ b/build.rs @@ -7,7 +7,10 @@ fn main() { println!("cargo:rustc-cfg=armv6m") } - if target.starts_with("thumbv7m") | target.starts_with("thumbv7em") { + if target.starts_with("thumbv7m") + | target.starts_with("thumbv7em") + | target.starts_with("thumbv8m") + { println!("cargo:rustc-cfg=armv7m") } 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), 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), quote!(rtfm::export::Queue(rtfm::export::iQueue::u8())), + None, ) }; @@ -77,6 +79,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec Vec ! { 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, + 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 { 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 { 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 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 Result<(), #ty> { #let_instant #body @@ -66,9 +68,11 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec Vec); + 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 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 { + 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) -> Option { + 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 { -- cgit v1.2.3 From 0e146f8d1142672725b6abb38478f503a9261c80 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 20 Aug 2019 15:11:24 +0200 Subject: adapt to changes in rtfm-syntax --- .travis.yml | 8 +++++++- macros/Cargo.toml | 6 +++--- macros/src/check.rs | 7 ++++--- macros/src/codegen.rs | 2 +- macros/src/codegen/hardware_tasks.rs | 1 + macros/src/codegen/idle.rs | 2 ++ macros/src/codegen/init.rs | 2 ++ macros/src/codegen/software_tasks.rs | 1 + macros/src/codegen/util.rs | 6 +++--- macros/src/lib.rs | 1 - tests/single.rs | 6 ++++-- 11 files changed, 28 insertions(+), 14 deletions(-) (limited to 'macros/src/codegen/idle.rs') diff --git a/.travis.yml b/.travis.yml index 31d10e84..ac5a7b8a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,20 +5,26 @@ matrix: # NOTE used to build docs on successful merges to master - env: TARGET=x86_64-unknown-linux-gnu + # MSRV + - env: TARGET=thumbv7m-none-eabi + rust: 1.36.0 + if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master) + - env: TARGET=thumbv6m-none-eabi if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master) - env: TARGET=thumbv7m-none-eabi if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master) + # compile-fail tests - env: TARGET=x86_64-unknown-linux-gnu rust: nightly if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master) + # heterogeneous multi-core support - env: TARGET=thumbv6m-none-eabi rust: nightly if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master) - - env: TARGET=thumbv7m-none-eabi rust: nightly if: (branch = staging OR branch = trying) OR (type = pull_request AND branch = master) diff --git a/macros/Cargo.toml b/macros/Cargo.toml index c4e897fa..ed7626f8 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -15,9 +15,9 @@ version = "0.5.0-alpha.1" proc-macro = true [dependencies] -proc-macro2 = "0.4.30" -quote = "0.6.12" -syn = "0.15.34" +proc-macro2 = "1" +quote = "1" +syn = "1" [dependencies.rtfm-syntax] git = "https://github.com/japaric/rtfm-syntax" diff --git a/macros/src/check.rs b/macros/src/check.rs index 85fda75b..0136370c 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -169,9 +169,10 @@ pub fn app<'a>(app: &'a App, analysis: &Analysis) -> parse::Result> { peripherals = if *x { Some(0) } else { None } } - CustomArg::UInt(x) if app.args.cores != 1 => { - peripherals = if *x < u64::from(app.args.cores) { - Some(*x as u8) + CustomArg::UInt(s) if app.args.cores != 1 => { + let x = s.parse::().ok(); + peripherals = if x.is_some() && x.unwrap() < app.args.cores { + Some(x.unwrap()) } else { return Err(parse::Error::new( k.span(), diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 8ac06d53..02138481 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -126,7 +126,7 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #(#root)* - #(#mod_resources)* + #mod_resources #(#root_hardware_tasks)* diff --git a/macros/src/codegen/hardware_tasks.rs b/macros/src/codegen/hardware_tasks.rs index cf92e078..a9c2a2bd 100644 --- a/macros/src/codegen/hardware_tasks.rs +++ b/macros/src/codegen/hardware_tasks.rs @@ -115,6 +115,7 @@ pub fn codegen( let stmts = &task.stmts; let section = util::link_section("text", core); // XXX shouldn't this have a cfg_core? + let locals_pat = locals_pat.iter(); user_tasks.push(quote!( #(#attrs)* #[allow(non_snake_case)] diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index d6560761..35a72523 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -58,6 +58,7 @@ pub fn codegen( let context = &idle.context; let stmts = &idle.stmts; let section = util::link_section("text", core); + let locals_pat = locals_pat.iter(); let user_idle = Some(quote!( #(#attrs)* #[allow(non_snake_case)] @@ -70,6 +71,7 @@ pub fn codegen( } )); + let locals_new = locals_new.iter(); let call_idle = quote!(#name( #(#locals_new,)* #name::Context::new(&rtfm::export::Priority::new(0)) diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 878c633e..9c8ce31c 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -83,6 +83,7 @@ pub fn codegen( let attrs = &init.attrs; let stmts = &init.stmts; let section = util::link_section("text", core); + let locals_pat = locals_pat.iter(); let user_init = Some(quote!( #(#attrs)* #cfg_core @@ -102,6 +103,7 @@ pub fn codegen( const_app = Some(constructor); } + let locals_new = locals_new.iter(); let call_init = Some(quote!(let late = #name(#(#locals_new,)* #name::Context::new(core.into()));)); diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 2960faf9..be1eb05c 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -168,6 +168,7 @@ pub fn codegen( let attrs = &task.attrs; let cfgs = &task.cfgs; let stmts = &task.stmts; + let locals_pat = locals_pat.iter(); user_tasks.push(quote!( #(#attrs)* #(#cfgs)* diff --git a/macros/src/codegen/util.rs b/macros/src/codegen/util.rs index f5f96dea..207272dc 100644 --- a/macros/src/codegen/util.rs +++ b/macros/src/codegen/util.rs @@ -3,13 +3,13 @@ use core::sync::atomic::{AtomicUsize, Ordering}; use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::quote; use rtfm_syntax::{ast::App, Context, Core}; -use syn::{ArgCaptured, Attribute, Ident, IntSuffix, LitInt}; +use syn::{Attribute, Ident, LitInt, PatType}; use crate::check::Extra; /// Turns `capacity` into an unsuffixed integer literal pub fn capacity_literal(capacity: u8) -> LitInt { - LitInt::new(u64::from(capacity), IntSuffix::None, Span::call_site()) + LitInt::new(&capacity.to_string(), Span::call_site()) } /// Turns `capacity` into a type-level (`typenum`) integer @@ -194,7 +194,7 @@ pub fn rendezvous_ident(core: Core) -> Ident { // // `inputs` could be &[`input: Foo`] OR &[`mut x: i32`, `ref y: i64`] pub fn regroup_inputs( - inputs: &[ArgCaptured], + inputs: &[PatType], ) -> ( // args e.g. &[`_0`], &[`_0: i32`, `_1: i64`] Vec, diff --git a/macros/src/lib.rs b/macros/src/lib.rs index ed55095d..7a436e7b 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,5 +1,4 @@ #![deny(warnings)] -#![recursion_limit = "128"] extern crate proc_macro; diff --git a/tests/single.rs b/tests/single.rs index 93addf6e..01b80312 100644 --- a/tests/single.rs +++ b/tests/single.rs @@ -8,8 +8,10 @@ fn ui() { config.mode = Mode::Ui; config.src_base = PathBuf::from("ui/single"); - config.target_rustcflags = - Some("--edition=2018 -L target/debug/deps -Z unstable-options --extern rtfm --extern lm3s6965".to_owned()); + config.target_rustcflags = Some( + "--edition=2018 -L target/debug/deps -Z unstable-options --extern rtfm --extern lm3s6965" + .to_owned(), + ); config.link_deps(); config.clean_rmeta(); -- cgit v1.2.3