diff options
author | 2020-10-05 18:25:15 +0200 | |
---|---|---|
committer | 2020-10-05 18:25:15 +0200 | |
commit | eec0908024d9b4127ed496b4781adc08000cc2c2 (patch) | |
tree | f128b3d91ea4ac0f171ab334d4517552a6803d01 /macros/src | |
parent | 9d2598dc071882a94b813ab1d9ddf49092546257 (diff) | |
parent | f493f21359ccb3ab4643d9470f3581532f47593a (diff) | |
download | rtic-eec0908024d9b4127ed496b4781adc08000cc2c2.tar.gz rtic-eec0908024d9b4127ed496b4781adc08000cc2c2.tar.zst rtic-eec0908024d9b4127ed496b4781adc08000cc2c2.zip |
Merge branch 'master' into always_late_resources
Diffstat (limited to 'macros/src')
-rw-r--r-- | macros/src/codegen.rs | 96 | ||||
-rw-r--r-- | macros/src/codegen/hardware_tasks.rs | 28 | ||||
-rw-r--r-- | macros/src/codegen/idle.rs | 29 | ||||
-rw-r--r-- | macros/src/codegen/init.rs | 30 | ||||
-rw-r--r-- | macros/src/codegen/module.rs | 8 | ||||
-rw-r--r-- | macros/src/codegen/resources.rs | 24 | ||||
-rw-r--r-- | macros/src/codegen/resources_struct.rs | 2 | ||||
-rw-r--r-- | macros/src/codegen/schedule.rs | 6 | ||||
-rw-r--r-- | macros/src/codegen/software_tasks.rs | 37 | ||||
-rw-r--r-- | macros/src/codegen/spawn.rs | 4 | ||||
-rw-r--r-- | macros/src/lib.rs | 8 | ||||
-rw-r--r-- | macros/src/tests/single.rs | 4 |
12 files changed, 202 insertions, 74 deletions
diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index fe4d59a6..f230d395 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -25,21 +25,36 @@ mod util; // TODO document the syntax here or in `rtic-syntax` pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { - let mut const_app = vec![]; + let mut mod_app = vec![]; + let mut mod_app_imports = vec![]; let mut mains = vec![]; let mut root = vec![]; let mut user = vec![]; + let mut imports = vec![]; // Generate the `main` function let assertion_stmts = assertions::codegen(analysis); let pre_init_stmts = pre_init::codegen(&app, analysis, extra); - let (const_app_init, root_init, user_init, call_init) = init::codegen(app, analysis, extra); + let (mod_app_init, root_init, user_init, user_init_imports, call_init) = + init::codegen(app, analysis, extra); let post_init_stmts = post_init::codegen(&app, analysis); - let (const_app_idle, root_idle, user_idle, call_idle) = idle::codegen(app, analysis, extra); + let (mod_app_idle, root_idle, user_idle, user_idle_imports, call_idle) = + idle::codegen(app, analysis, extra); + + if user_init.is_some() { + mod_app_imports.push(quote!( + use super::init; + )) + } + if user_idle.is_some() { + mod_app_imports.push(quote!( + use super::idle; + )) + } user.push(quote!( #user_init @@ -47,16 +62,21 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #user_idle )); + imports.push(quote!( + #(#user_init_imports)* + #(#user_idle_imports)* + )); + root.push(quote!( #(#root_init)* #(#root_idle)* )); - const_app.push(quote!( - #const_app_init + mod_app.push(quote!( + #mod_app_init - #const_app_idle + #mod_app_idle )); let main = util::suffixed("main"); @@ -77,22 +97,33 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { } )); - let (const_app_resources, mod_resources) = resources::codegen(app, analysis, extra); + let (mod_app_resources, mod_resources, mod_resources_imports) = + resources::codegen(app, analysis, extra); - let (const_app_hardware_tasks, root_hardware_tasks, user_hardware_tasks) = - hardware_tasks::codegen(app, analysis, extra); + let ( + mod_app_hardware_tasks, + root_hardware_tasks, + user_hardware_tasks, + user_hardware_tasks_imports, + ) = hardware_tasks::codegen(app, analysis, extra); - let (const_app_software_tasks, root_software_tasks, user_software_tasks) = - software_tasks::codegen(app, analysis, extra); + let ( + mod_app_software_tasks, + root_software_tasks, + user_software_tasks, + user_software_tasks_imports, + ) = software_tasks::codegen(app, analysis, extra); - let const_app_dispatchers = dispatchers::codegen(app, analysis, extra); + let mod_app_dispatchers = dispatchers::codegen(app, analysis, extra); - let const_app_spawn = spawn::codegen(app, analysis, extra); + let mod_app_spawn = spawn::codegen(app, analysis, extra); - let const_app_timer_queue = timer_queue::codegen(app, analysis, extra); + let mod_app_timer_queue = timer_queue::codegen(app, analysis, extra); - let const_app_schedule = schedule::codegen(app, extra); + let mod_app_schedule = schedule::codegen(app, extra); + let user_imports = app.user_imports.clone(); + let user_code = app.user_code.clone(); let name = &app.name; let device = extra.device; quote!( @@ -111,28 +142,41 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #(#root_software_tasks)* /// Implementation details - // The user can't access the items within this `const` item - const #name: () = { + mod #name { /// Always include the device crate which contains the vector table use #device as _; + #(#imports)* + #(#user_imports)* + + /// User code from within the module + #(#user_code)* + /// User code end + - #(#const_app)* + #(#user_hardware_tasks_imports)* - #(#const_app_resources)* + #(#user_software_tasks_imports)* - #(#const_app_hardware_tasks)* + #(#mod_resources_imports)* - #(#const_app_software_tasks)* + /// app module + #(#mod_app)* - #(#const_app_dispatchers)* + #(#mod_app_resources)* - #(#const_app_spawn)* + #(#mod_app_hardware_tasks)* - #(#const_app_timer_queue)* + #(#mod_app_software_tasks)* - #(#const_app_schedule)* + #(#mod_app_dispatchers)* + + #(#mod_app_spawn)* + + #(#mod_app_timer_queue)* + + #(#mod_app_schedule)* #(#mains)* - }; + } ) } diff --git a/macros/src/codegen/hardware_tasks.rs b/macros/src/codegen/hardware_tasks.rs index 7f14b5e1..25f1df41 100644 --- a/macros/src/codegen/hardware_tasks.rs +++ b/macros/src/codegen/hardware_tasks.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::quote; +use quote::{format_ident, quote}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -14,7 +14,7 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app_hardware_tasks -- interrupt handlers and `${task}Resources` constructors + // mod_app_hardware_tasks -- interrupt handlers and `${task}Resources` constructors Vec<TokenStream2>, // root_hardware_tasks -- items that must be placed in the root of the crate: // - `${task}Locals` structs @@ -23,10 +23,13 @@ pub fn codegen( Vec<TokenStream2>, // user_hardware_tasks -- the `#[task]` functions written by the user Vec<TokenStream2>, + // user_hardware_tasks_imports -- the imports for `#[task]` functions written by the user + Vec<TokenStream2>, ) { - let mut const_app = vec![]; + let mut mod_app = vec![]; let mut root = vec![]; let mut user_tasks = vec![]; + let mut hardware_tasks_imports = vec![]; for (name, task) in &app.hardware_tasks { let (let_instant, instant) = if app.uses_schedule() { @@ -49,7 +52,7 @@ pub fn codegen( let symbol = task.args.binds.clone(); let priority = task.args.priority; - const_app.push(quote!( + mod_app.push(quote!( #[allow(non_snake_case)] #[no_mangle] unsafe fn #symbol() { @@ -78,9 +81,16 @@ pub fn codegen( analysis, ); + // Add resources to imports + let name_res = format_ident!("{}Resources", name); + hardware_tasks_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_res; + )); + root.push(item); - const_app.push(constructor); + mod_app.push(constructor); } root.push(module::codegen( @@ -112,7 +122,13 @@ pub fn codegen( #(#stmts)* } )); + + hardware_tasks_imports.push(quote!( + #(#attrs)* + #[allow(non_snake_case)] + use super::#name; + )); } - (const_app, root, user_tasks) + (mod_app, root, user_tasks, hardware_tasks_imports) } diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index d0bff3e7..2e2932d7 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::quote; +use quote::{format_ident, quote}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -14,7 +14,7 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app_idle -- the `${idle}Resources` constructor + // mod_app_idle -- the `${idle}Resources` constructor Option<TokenStream2>, // root_idle -- items that must be placed in the root of the crate: // - the `${idle}Locals` struct @@ -23,26 +23,37 @@ pub fn codegen( Vec<TokenStream2>, // user_idle Option<TokenStream2>, + // user_idle_imports + Vec<TokenStream2>, // call_idle TokenStream2, ) { if app.idles.len() > 0 { let idle = &app.idles.first().unwrap(); let mut needs_lt = false; - let mut const_app = None; + let mut mod_app = None; let mut root_idle = vec![]; let mut locals_pat = None; let mut locals_new = None; + let mut user_idle_imports = vec![]; + + let name = &idle.name; + if !idle.args.resources.is_empty() { let (item, constructor) = resources_struct::codegen(Context::Idle, 0, &mut needs_lt, app, analysis); root_idle.push(item); - const_app = Some(constructor); + mod_app = Some(constructor); + + let name_resource = format_ident!("{}Resources", name); + user_idle_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_resource; + )); } - let name = &idle.name; if !idle.locals.is_empty() { let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app); @@ -66,6 +77,11 @@ pub fn codegen( #(#stmts)* } )); + user_idle_imports.push(quote!( + #(#attrs)* + #[allow(non_snake_case)] + use super::#name; + )); let locals_new = locals_new.iter(); let call_idle = quote!(crate::#name( @@ -73,12 +89,13 @@ pub fn codegen( #name::Context::new(&rtic::export::Priority::new(0)) )); - (const_app, root_idle, user_idle, call_idle) + (mod_app, root_idle, user_idle, user_idle_imports, call_idle) } else { ( None, vec![], None, + vec![], quote!(loop { rtic::export::wfi() }), diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index b41c3894..1ced68bc 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::quote; +use quote::{format_ident, quote}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -14,7 +14,7 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app_idle -- the `${init}Resources` constructor + // mod_app_idle -- the `${init}Resources` constructor Option<TokenStream2>, // root_init -- items that must be placed in the root of the crate: // - the `${init}Locals` struct @@ -24,6 +24,8 @@ pub fn codegen( Vec<TokenStream2>, // user_init -- the `#[init]` function written by the user Option<TokenStream2>, + // user_init_imports -- the imports for `#[init]` functio written by the user + Vec<TokenStream2>, // call_init -- the call to the user `#[init]` if there's one Option<TokenStream2>, ) { @@ -43,13 +45,14 @@ pub fn codegen( let cfgs = &app.late_resources[name].cfgs; quote!( - #(#cfgs)* - pub #name: #ty + #(#cfgs)* + pub #name: #ty ) }) }) .collect::<Vec<_>>(); + let mut user_init_imports = vec![]; let late_resources = util::late_resources_ident(&name); root_init.push(quote!( @@ -81,14 +84,25 @@ pub fn codegen( #(#stmts)* } )); + user_init_imports.push(quote!( + #(#attrs)* + #[allow(non_snake_case)] + use super::#name; + )); - let mut const_app = None; + let mut mod_app = None; if !init.args.resources.is_empty() { let (item, constructor) = resources_struct::codegen(Context::Init, 0, &mut needs_lt, app, analysis); root_init.push(item); - const_app = Some(constructor); + mod_app = Some(constructor); + + let name_late = format_ident!("{}Resources", name); + user_init_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_late; + )); } let locals_new = locals_new.iter(); @@ -98,8 +112,8 @@ pub fn codegen( root_init.push(module::codegen(Context::Init, needs_lt, app, extra)); - (const_app, root_init, user_init, call_init) + (mod_app, root_init, user_init, user_init_imports, call_init) } else { - (None, vec![], None, None) + (None, vec![], None, vec![], None) } } diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs index 85bab3ab..2e51e7db 100644 --- a/macros/src/codegen/module.rs +++ b/macros/src/codegen/module.rs @@ -47,6 +47,14 @@ pub fn codegen(ctxt: Context, resources_tick: bool, app: &App, extra: &Extra) -> values.push(quote!(device: #device::Peripherals::steal())); } + lt = Some(quote!('a)); + fields.push(quote!( + /// Critical section token for init + pub cs: rtic::export::CriticalSection<#lt> + )); + + values.push(quote!(cs: rtic::export::CriticalSection::new())); + values.push(quote!(core)); } diff --git a/macros/src/codegen/resources.rs b/macros/src/codegen/resources.rs index 4196ee7a..38ea5245 100644 --- a/macros/src/codegen/resources.rs +++ b/macros/src/codegen/resources.rs @@ -10,13 +10,16 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app -- the `static [mut]` variables behind the proxies + // mod_app -- the `static [mut]` variables behind the proxies Vec<TokenStream2>, // mod_resources -- the `resources` module TokenStream2, + // mod_resources_imports -- the `resources` module imports + Vec<TokenStream2>, ) { - let mut const_app = vec![]; + let mut mod_app = vec![]; let mut mod_resources = vec![]; + let mut mod_resources_imports = vec![]; for (name, res, expr, _) in app.resources(analysis) { let cfgs = &res.cfgs; @@ -39,7 +42,7 @@ pub fn codegen( }; let attrs = &res.attrs; - const_app.push(quote!( + mod_app.push(quote!( #[allow(non_upper_case_globals)] #(#attrs)* #(#cfgs)* @@ -82,7 +85,13 @@ pub fn codegen( ) }; - const_app.push(util::impl_mutex( + mod_resources_imports.push(quote!( + #[allow(non_camel_case_types)] + #(#cfgs)* + use super::resources::#name; + )); + + mod_app.push(util::impl_mutex( extra, cfgs, true, @@ -97,6 +106,11 @@ pub fn codegen( let mod_resources = if mod_resources.is_empty() { quote!() } else { + // Also import the resource module + mod_resources_imports.push(quote!( + use super::resources; + )); + quote!(mod resources { use rtic::export::Priority; @@ -104,5 +118,5 @@ pub fn codegen( }) }; - (const_app, mod_resources) + (mod_app, mod_resources, mod_resources_imports) } diff --git a/macros/src/codegen/resources_struct.rs b/macros/src/codegen/resources_struct.rs index dbbba30e..92d5b666 100644 --- a/macros/src/codegen/resources_struct.rs +++ b/macros/src/codegen/resources_struct.rs @@ -165,7 +165,7 @@ pub fn codegen( let constructor = quote!( impl<#lt> #ident<#lt> { #[inline(always)] - unsafe fn new(#arg) -> Self { + pub unsafe fn new(#arg) -> Self { #ident { #(#values,)* } diff --git a/macros/src/codegen/schedule.rs b/macros/src/codegen/schedule.rs index 46b0f384..5a887496 100644 --- a/macros/src/codegen/schedule.rs +++ b/macros/src/codegen/schedule.rs @@ -34,7 +34,7 @@ pub fn codegen(app: &App, extra: &Extra) -> Vec<TokenStream2> { methods.push(quote!( #(#cfgs)* - fn #name(&self, instant: #instant #(,#args)*) -> Result<(), #ty> { + pub fn #name(&self, instant: #instant #(,#args)*) -> Result<(), #ty> { #body } )); @@ -49,7 +49,7 @@ pub fn codegen(app: &App, extra: &Extra) -> Vec<TokenStream2> { items.push(quote!( #(#cfgs)* - unsafe fn #schedule( + pub unsafe fn #schedule( priority: &rtic::export::Priority, instant: #instant #(,#args)* @@ -62,7 +62,7 @@ pub fn codegen(app: &App, extra: &Extra) -> Vec<TokenStream2> { methods.push(quote!( #(#cfgs)* #[inline(always)] - fn #name(&self, instant: #instant #(,#args)*) -> Result<(), #ty> { + pub fn #name(&self, instant: #instant #(,#args)*) -> Result<(), #ty> { unsafe { #schedule(self.priority(), instant #(,#untupled)*) } diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index b56db419..4ae37e4e 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::quote; +use quote::{format_ident, quote}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -13,7 +13,7 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app_software_tasks -- free queues, buffers and `${task}Resources` constructors + // mod_app_software_tasks -- free queues, buffers and `${task}Resources` constructors Vec<TokenStream2>, // root_software_tasks -- items that must be placed in the root of the crate: // - `${task}Locals` structs @@ -22,10 +22,13 @@ pub fn codegen( Vec<TokenStream2>, // user_software_tasks -- the `#[task]` functions written by the user Vec<TokenStream2>, + // user_software_tasks_imports -- the imports for `#[task]` functions written by the user + Vec<TokenStream2>, ) { - let mut const_app = vec![]; + let mut mod_app = vec![]; let mut root = vec![]; let mut user_tasks = vec![]; + let mut software_tasks_imports = vec![]; for (name, task) in &app.software_tasks { let inputs = &task.inputs; @@ -48,7 +51,7 @@ pub fn codegen( Box::new(|| util::link_section_uninit(true)), ) }; - const_app.push(quote!( + mod_app.push(quote!( /// Queue version of a free-list that keeps track of empty slots in /// the following buffers static mut #fq: #fq_ty = #fq_expr; @@ -56,13 +59,13 @@ pub fn codegen( // Generate a resource proxy if needed if let Some(ceiling) = ceiling { - const_app.push(quote!( + mod_app.push(quote!( struct #fq<'a> { priority: &'a rtic::export::Priority, } )); - const_app.push(util::impl_mutex( + mod_app.push(util::impl_mutex( extra, &[], false, @@ -82,7 +85,7 @@ pub fn codegen( let instants = util::instants_ident(name); let uninit = mk_uninit(); - const_app.push(quote!( + mod_app.push(quote!( #uninit /// Buffer that holds the instants associated to the inputs of a task static mut #instants: @@ -93,7 +96,7 @@ pub fn codegen( let uninit = mk_uninit(); let inputs = util::inputs_ident(name); - const_app.push(quote!( + mod_app.push(quote!( #uninit /// Buffer that holds the inputs of a task static mut #inputs: [core::mem::MaybeUninit<#input_ty>; #cap_lit] = @@ -112,9 +115,16 @@ pub fn codegen( analysis, ); + // Add resources to imports + let name_res = format_ident!("{}Resources", name); + software_tasks_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_res; + )); + root.push(item); - const_app.push(constructor); + mod_app.push(constructor); } // `${task}Locals` @@ -135,12 +145,17 @@ pub fn codegen( #(#attrs)* #(#cfgs)* #[allow(non_snake_case)] - fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) { + pub fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) { use rtic::Mutex as _; #(#stmts)* } )); + software_tasks_imports.push(quote!( + #(#cfgs)* + #[allow(non_snake_case)] + use super::#name; + )); root.push(module::codegen( Context::SoftwareTask(name), @@ -150,5 +165,5 @@ pub fn codegen( )); } - (const_app, root, user_tasks) + (mod_app, root, user_tasks, software_tasks_imports) } diff --git a/macros/src/codegen/spawn.rs b/macros/src/codegen/spawn.rs index 4b824f56..da281516 100644 --- a/macros/src/codegen/spawn.rs +++ b/macros/src/codegen/spawn.rs @@ -40,7 +40,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream methods.push(quote!( #(#cfgs)* - fn #name(&self #(,#args)*) -> Result<(), #ty> { + pub fn #name(&self #(,#args)*) -> Result<(), #ty> { #let_instant #body } @@ -92,7 +92,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream methods.push(quote!( #(#cfgs)* #[inline(always)] - fn #name(&self #(,#args)*) -> Result<(), #ty> { + pub fn #name(&self #(,#args)*) -> Result<(), #ty> { unsafe { #let_instant #spawn(self.priority() #instant #(,#untupled)*) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 7b72c7fb..94e7eec6 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -15,8 +15,7 @@ mod tests; /// Attribute used to declare a RTIC application /// -/// This attribute must be applied to a `const` item of type `()`. The `const` item is effectively -/// used as a `mod` item: its value must be a block that contains items commonly found in modules, +/// This attribute must be applied to a module block that contains items commonly found in modules, /// like functions and `static` variables. /// /// The `app` attribute has one mandatory argument: @@ -34,9 +33,10 @@ mod tests; /// - `monotonic = <path>`. This is a path to a zero-sized structure (e.g. `struct Foo;`) that /// implements the `Monotonic` trait. This argument must be provided to use the `schedule` API. /// -/// The items allowed in the block value of the `const` item are specified below: +/// The items allowed in the module block are specified below: /// -/// # 1. `struct Resources` +/// # 1. `#[resources] +/// struct <resource-name>` /// /// This structure contains the declaration of all the resources used by the application. Each field /// in this structure corresponds to a different resource. Each resource may optionally be given an diff --git a/macros/src/tests/single.rs b/macros/src/tests/single.rs index d6693882..97cbbb3f 100644 --- a/macros/src/tests/single.rs +++ b/macros/src/tests/single.rs @@ -8,7 +8,7 @@ fn analyze() { let (app, analysis) = rtic_syntax::parse2( quote!(device = pac), quote!( - const APP: () = { + mod app { #[task(priority = 1)] fn a(_: a::Context) {} @@ -20,7 +20,7 @@ fn analyze() { fn B(); fn A(); } - }; + } ), settings, ) |