From 3f85cb5caf1ae930e6551e139978ceec859a2348 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Mon, 5 Jul 2021 21:40:01 +0200 Subject: Started work --- macros/src/codegen/shared_resources.rs | 145 +++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 macros/src/codegen/shared_resources.rs (limited to 'macros/src/codegen/shared_resources.rs') diff --git a/macros/src/codegen/shared_resources.rs b/macros/src/codegen/shared_resources.rs new file mode 100644 index 00000000..f6d2dcb0 --- /dev/null +++ b/macros/src/codegen/shared_resources.rs @@ -0,0 +1,145 @@ +use proc_macro2::TokenStream as TokenStream2; +use quote::quote; +use rtic_syntax::{analyze::Ownership, ast::App}; + +use crate::{analyze::Analysis, check::Extra, codegen::util}; + +/// Generates `static` variables and shared resource proxies +pub fn codegen( + app: &App, + analysis: &Analysis, + extra: &Extra, +) -> ( + // mod_app -- the `static` variables behind the proxies + Vec, + // mod_resources -- the `resources` module + TokenStream2, +) { + let mut mod_app = vec![]; + let mut mod_resources = vec![]; + + for (name, res) in app.shared_resources { + let cfgs = &res.cfgs; + let ty = &res.ty; + let mangled_name = util::mark_internal_ident(&name); + + { + // late resources in `util::link_section_uninit` + let section = if expr.is_none() { + util::link_section_uninit(true) + } else { + None + }; + + // resource type and assigned value + let (ty, expr) = if let Some(expr) = expr { + // early resource + ( + quote!(rtic::RacyCell<#ty>), + quote!(rtic::RacyCell::new(#expr)), + ) + } else { + // late resource + ( + quote!(rtic::RacyCell>), + quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit())), + ) + }; + + let attrs = &res.attrs; + + // For future use + // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); + mod_app.push(quote!( + #[allow(non_upper_case_globals)] + // #[doc = #doc] + #[doc(hidden)] + #(#attrs)* + #(#cfgs)* + #section + static #mangled_name: #ty = #expr; + )); + } + + let r_prop = &res.properties; + // For future use + // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); + + if !r_prop.task_local && !r_prop.lock_free { + mod_resources.push(quote!( + // #[doc = #doc] + #[doc(hidden)] + #[allow(non_camel_case_types)] + #(#cfgs)* + pub struct #name<'a> { + priority: &'a Priority, + } + + #(#cfgs)* + impl<'a> #name<'a> { + #[inline(always)] + pub unsafe fn new(priority: &'a Priority) -> Self { + #name { priority } + } + + #[inline(always)] + pub unsafe fn priority(&self) -> &Priority { + self.priority + } + } + )); + + let (ptr, _doc) = if expr.is_none() { + // late resource + ( + quote!( + #(#cfgs)* + #mangled_name.get_mut_unchecked().as_mut_ptr() + ), + "late", + ) + } else { + // early resource + ( + quote!( + #(#cfgs)* + #mangled_name.get_mut_unchecked() + ), + "early", + ) + }; + + let ceiling = match analysis.ownerships.get(name) { + Some(Ownership::Owned { priority }) => *priority, + Some(Ownership::CoOwned { priority }) => *priority, + Some(Ownership::Contended { ceiling }) => *ceiling, + None => 0, + }; + + // For future use + // let doc = format!(" RTIC internal ({} resource): {}:{}", doc, file!(), line!()); + + mod_app.push(util::impl_mutex( + extra, + cfgs, + true, + name, + quote!(#ty), + ceiling, + ptr, + )); + } + } + + let mod_resources = if mod_resources.is_empty() { + quote!() + } else { + quote!(mod resources { + use rtic::export::Priority; + + #(#mod_resources)* + }) + }; + + (mod_app, mod_resources) +} -- cgit v1.2.3 From ef5307d83a1d62df0569d78db75d4006147c927d Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Tue, 6 Jul 2021 22:47:48 +0200 Subject: Minimal app now compiles --- macros/src/codegen.rs | 11 +- macros/src/codegen/dispatchers.rs | 12 +-- macros/src/codegen/hardware_tasks.rs | 48 +++++---- macros/src/codegen/idle.rs | 29 +++--- macros/src/codegen/init.rs | 130 ++++++++++++++--------- macros/src/codegen/local_resources.rs | 145 ++++++-------------------- macros/src/codegen/local_resources_struct.rs | 4 +- macros/src/codegen/module.rs | 69 ++++++------ macros/src/codegen/post_init.rs | 51 +++++---- macros/src/codegen/pre_init.rs | 2 +- macros/src/codegen/shared_resources.rs | 84 ++++----------- macros/src/codegen/shared_resources_struct.rs | 6 +- macros/src/codegen/software_tasks.rs | 25 +++-- macros/src/codegen/util.rs | 16 ++- 14 files changed, 284 insertions(+), 348 deletions(-) (limited to 'macros/src/codegen/shared_resources.rs') diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index f6fdf022..081c37fa 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -84,7 +84,8 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { } )); - let (mod_app_resources, mod_resources) = resources::codegen(app, analysis, extra); + let (mod_app_shared_resources, mod_shared_resources) = shared_resources::codegen(app, analysis, extra); + let (mod_app_local_resources, mod_local_resources) = local_resources::codegen(app, analysis, extra); let (mod_app_hardware_tasks, root_hardware_tasks, user_hardware_tasks) = hardware_tasks::codegen(app, analysis, extra); @@ -186,7 +187,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #(#root)* - #mod_resources + #mod_shared_resources + + #mod_local_resources #(#root_hardware_tasks)* @@ -195,7 +198,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { /// app module #(#mod_app)* - #(#mod_app_resources)* + #(#mod_app_shared_resources)* + + #(#mod_app_local_resources)* #(#mod_app_hardware_tasks)* diff --git a/macros/src/codegen/dispatchers.rs b/macros/src/codegen/dispatchers.rs index dfa52d5d..1c311954 100644 --- a/macros/src/codegen/dispatchers.rs +++ b/macros/src/codegen/dispatchers.rs @@ -76,11 +76,12 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec Vec ! { + fn #name(#context: #name::Context) -> ! { use rtic::Mutex as _; use rtic::mutex_prelude::*; @@ -74,9 +71,7 @@ pub fn codegen( } )); - let locals_new = locals_new.iter(); let call_idle = quote!(#name( - #(#locals_new,)* #name::Context::new(&rtic::export::Priority::new(0)) )); diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index fe8a1263..e3f74086 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context}; use crate::{ analyze::Analysis, check::Extra, - codegen::{locals, module, resources_struct}, + codegen::{local_resources_struct, module}, }; type CodegenResult = ( @@ -18,68 +18,96 @@ type CodegenResult = ( // - the `${init}` module, which contains types like `${init}::Context` Vec, // user_init -- the `#[init]` function written by the user - Option, - // call_init -- the call to the user `#[init]` if there's one - Option, + TokenStream2, + // call_init -- the call to the user `#[init]` + TokenStream2, ); /// Generates support code for `#[init]` functions pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { - if !app.inits.is_empty() { - let init = &app.inits.first().unwrap(); - let mut needs_lt = false; - let name = &init.name; + let init = &app.init; + let mut needs_lt = false; + let name = &init.name; - let mut root_init = vec![]; + let mut root_init = vec![]; - let mut locals_pat = None; - let mut locals_new = None; - if !init.locals.is_empty() { - let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app); + // TODO: Fix locals + // let mut locals_pat = None; + // let mut locals_new = None; + // if !init.locals.is_empty() { + // let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app); - locals_new = Some(quote!(#name::Locals::new())); - locals_pat = Some(pat); - root_init.push(struct_); - } + // locals_new = Some(quote!(#name::Locals::new())); + // locals_pat = Some(pat); + // root_init.push(struct_); + // } - let context = &init.context; - let attrs = &init.attrs; - let stmts = &init.stmts; - let locals_pat = locals_pat.iter(); + let context = &init.context; + let attrs = &init.attrs; + let stmts = &init.stmts; + let shared = &init.user_shared_struct; + let local = &init.user_local_struct; - let user_init_return = quote! {#name::LateResources, #name::Monotonics}; + let shared_resources: Vec<_> = app + .shared_resources + .iter() + .map(|(k, v)| { + let ty = &v.ty; + let cfgs = &v.cfgs; + quote!( + #(#cfgs)* + #k: #ty, + ) + }) + .collect(); + let local_resources: Vec<_> = app + .local_resources + .iter() + .map(|(k, v)| { + let ty = &v.ty; + let cfgs = &v.cfgs; + quote!( + #(#cfgs)* + #k: #ty, + ) + }) + .collect(); + root_init.push(quote! { + struct #shared { + #(#shared_resources)* + } - let user_init = Some(quote!( - #(#attrs)* - #[allow(non_snake_case)] - fn #name(#(#locals_pat,)* #context: #name::Context) -> (#user_init_return) { - #(#stmts)* - } - )); + struct #local { + #(#local_resources)* + } + }); - let mut mod_app = None; - if !init.args.resources.is_empty() { - let (item, constructor) = resources_struct::codegen(Context::Init, &mut needs_lt, app); + // let locals_pat = locals_pat.iter(); - root_init.push(item); - mod_app = Some(constructor); + let user_init_return = quote! {#shared, #local, #name::Monotonics}; + + let user_init = quote!( + #(#attrs)* + #[allow(non_snake_case)] + fn #name(#context: #name::Context) -> (#user_init_return) { + #(#stmts)* } + ); + + let mut mod_app = None; + + // let locals_new = locals_new.iter(); + let call_init = quote! { + let (shared_resources, local_resources, mut monotonics) = #name(#name::Context::new(core.into())); + }; + + root_init.push(module::codegen( + Context::Init, + needs_lt, + app, + analysis, + extra, + )); - let locals_new = locals_new.iter(); - let call_init = Some( - quote!(let (late, mut monotonics) = #name(#(#locals_new,)* #name::Context::new(core.into()));), - ); - - root_init.push(module::codegen( - Context::Init, - needs_lt, - app, - analysis, - extra, - )); - - (mod_app, root_init, user_init, call_init) - } else { - (None, vec![], None, None) - } + (mod_app, root_init, user_init, call_init) } diff --git a/macros/src/codegen/local_resources.rs b/macros/src/codegen/local_resources.rs index dd3cbb44..13891f93 100644 --- a/macros/src/codegen/local_resources.rs +++ b/macros/src/codegen/local_resources.rs @@ -1,10 +1,12 @@ use proc_macro2::TokenStream as TokenStream2; use quote::quote; -use rtic_syntax::{analyze::Ownership, ast::App}; +use rtic_syntax::ast::App; use crate::{analyze::Analysis, check::Extra, codegen::util}; /// Generates `local` variables and local resource proxies +/// +/// I.e. the `static` variables and theirs proxies. pub fn codegen( app: &App, analysis: &Analysis, @@ -16,131 +18,42 @@ pub fn codegen( TokenStream2, ) { let mut mod_app = vec![]; - let mut mod_resources = vec![]; + // let mut mod_resources: _ = vec![]; - for (name, res) in app.local_resources { + // All local resources declared in the `#[local]' struct + for (name, res) in &app.local_resources { // let expr = &res.expr; // TODO: Extract from tasks???... let cfgs = &res.cfgs; let ty = &res.ty; - let mangled_name = util::mark_internal_ident(&name); + let mangled_name = util::mark_internal_ident(&util::static_local_resource_ident(name)); - { - // late resources in `util::link_section_uninit` - let section = if expr.is_none() { - util::link_section_uninit(true) - } else { - None - }; + let ty = quote!(rtic::RacyCell>); + let expr = quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit())); - // resource type and assigned value - let (ty, expr) = if let Some(expr) = expr { - // early resource - ( - quote!(rtic::RacyCell<#ty>), - quote!(rtic::RacyCell::new(#expr)), - ) - } else { - // late resource - ( - quote!(rtic::RacyCell>), - quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit())), - ) - }; + let attrs = &res.attrs; + // late resources in `util::link_section_uninit` + let section = util::link_section_uninit(true); - let attrs = &res.attrs; - - // For future use - // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); - mod_app.push(quote!( - #[allow(non_upper_case_globals)] - // #[doc = #doc] - #[doc(hidden)] - #(#attrs)* - #(#cfgs)* - #section - static #mangled_name: #ty = #expr; - )); - } - - let r_prop = &res.properties; // For future use // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); - - if !r_prop.task_local && !r_prop.lock_free { - mod_resources.push(quote!( - // #[doc = #doc] - #[doc(hidden)] - #[allow(non_camel_case_types)] - #(#cfgs)* - pub struct #name<'a> { - priority: &'a Priority, - } - - #(#cfgs)* - impl<'a> #name<'a> { - #[inline(always)] - pub unsafe fn new(priority: &'a Priority) -> Self { - #name { priority } - } - - #[inline(always)] - pub unsafe fn priority(&self) -> &Priority { - self.priority - } - } - )); - - let (ptr, _doc) = if expr.is_none() { - // late resource - ( - quote!( - #(#cfgs)* - #mangled_name.get_mut_unchecked().as_mut_ptr() - ), - "late", - ) - } else { - // early resource - ( - quote!( - #(#cfgs)* - #mangled_name.get_mut_unchecked() - ), - "early", - ) - }; - - let ceiling = match analysis.ownerships.get(name) { - Some(Ownership::Owned { priority }) => *priority, - Some(Ownership::CoOwned { priority }) => *priority, - Some(Ownership::Contended { ceiling }) => *ceiling, - None => 0, - }; - - // For future use - // let doc = format!(" RTIC internal ({} resource): {}:{}", doc, file!(), line!()); - - mod_app.push(util::impl_mutex( - extra, - cfgs, - true, - name, - quote!(#ty), - ceiling, - ptr, - )); - } + mod_app.push(quote!( + #[allow(non_upper_case_globals)] + // #[doc = #doc] + #[doc(hidden)] + #(#attrs)* + #(#cfgs)* + #section + static #mangled_name: #ty = #expr; + )); } - let mod_resources = if mod_resources.is_empty() { - quote!() - } else { - quote!(mod resources { - use rtic::export::Priority; - - #(#mod_resources)* - }) - }; + // let mod_resources = if mod_resources.is_empty() { + // quote!() + // } else { + // quote!(mod local_resources { + // #(#mod_resources)* + // }) + // }; - (mod_app, mod_resources) + (mod_app, TokenStream2::new()) } diff --git a/macros/src/codegen/local_resources_struct.rs b/macros/src/codegen/local_resources_struct.rs index 3016f379..14706a5b 100644 --- a/macros/src/codegen/local_resources_struct.rs +++ b/macros/src/codegen/local_resources_struct.rs @@ -10,7 +10,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, let resources = match ctxt { Context::Init => &app.init.args.local_resources, - Context::Idle => &app.idle.unwrap().args.local_resources, + Context::Idle => &app.idle.as_ref().unwrap().args.local_resources, Context::HardwareTask(name) => &app.hardware_tasks[name].args.local_resources, Context::SoftwareTask(name) => &app.software_tasks[name].args.local_resources, }; @@ -33,7 +33,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, }; let ty = &res.ty; - let mangled_name = util::mark_internal_ident(&name); + let mangled_name = util::mark_internal_ident(&util::static_local_resource_ident(name)); fields.push(quote!( #(#cfgs)* diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs index a3d3fab2..adf64d5b 100644 --- a/macros/src/codegen/module.rs +++ b/macros/src/codegen/module.rs @@ -56,16 +56,39 @@ pub fn codegen( Context::SoftwareTask(_) => {} } - if ctxt.has_locals(app) { - let ident = util::locals_ident(ctxt, app); + // if ctxt.has_locals(app) { + // let ident = util::locals_ident(ctxt, app); + // module_items.push(quote!( + // #[doc(inline)] + // pub use super::#ident as Locals; + // )); + // } + + if ctxt.has_local_resources(app) { + let ident = util::local_resources_ident(ctxt, app); + let ident = util::mark_internal_ident(&ident); + let lt = if resources_tick { + lt = Some(quote!('a)); + Some(quote!('a)) + } else { + None + }; + module_items.push(quote!( #[doc(inline)] - pub use super::#ident as Locals; + pub use super::#ident as LocalResources; + )); + + fields.push(quote!( + /// Local Resources this task has access to + pub local: #name::LocalResources<#lt> )); + + values.push(quote!(local: #name::LocalResources::new())); } - if ctxt.has_resources(app) { - let ident = util::resources_ident(ctxt, app); + if ctxt.has_shared_resources(app) { + let ident = util::shared_resources_ident(ctxt, app); let ident = util::mark_internal_ident(&ident); let lt = if resources_tick { lt = Some(quote!('a)); @@ -76,12 +99,12 @@ pub fn codegen( module_items.push(quote!( #[doc(inline)] - pub use super::#ident as Resources; + pub use super::#ident as SharedResources; )); fields.push(quote!( - /// Resources this task has access to - pub resources: #name::Resources<#lt> + /// Shared Resources this task has access to + pub shared: #name::SharedResources<#lt> )); let priority = if ctxt.is_init() { @@ -89,38 +112,10 @@ pub fn codegen( } else { Some(quote!(priority)) }; - values.push(quote!(resources: #name::Resources::new(#priority))); + values.push(quote!(shared: #name::SharedResources::new(#priority))); } if let Context::Init = ctxt { - let late_fields = analysis - .late_resources - .iter() - .flat_map(|resources| { - resources.iter().map(|name| { - let ty = &app.late_resources[name].ty; - let cfgs = &app.late_resources[name].cfgs; - - quote!( - #(#cfgs)* - pub #name: #ty - ) - }) - }) - .collect::>(); - - let internal_late_ident = util::mark_internal_name("LateResources"); - items.push(quote!( - /// Resources initialized at runtime - #[allow(non_snake_case)] - pub struct #internal_late_ident { - #(#late_fields),* - } - )); - module_items.push(quote!( - pub use super::#internal_late_ident as LateResources; - )); - let monotonic_types: Vec<_> = app .monotonics .iter() diff --git a/macros/src/codegen/post_init.rs b/macros/src/codegen/post_init.rs index 78548bcd..161068d2 100644 --- a/macros/src/codegen/post_init.rs +++ b/macros/src/codegen/post_init.rs @@ -9,24 +9,39 @@ use crate::{analyze::Analysis, codegen::util}; pub fn codegen(app: &App, analysis: &Analysis) -> Vec { let mut stmts = vec![]; - // Initialize late resources - if !analysis.late_resources.is_empty() { - // BTreeSet wrapped in a vector - for name in analysis.late_resources.first().unwrap() { - let mangled_name = util::mark_internal_ident(&name); - // If it's live - let cfgs = app.late_resources[name].cfgs.clone(); - if analysis.locations.get(name).is_some() { - stmts.push(quote!( - // We include the cfgs - #(#cfgs)* - // Late resource is a RacyCell> - // - `get_mut_unchecked` to obtain `MaybeUninit` - // - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit` - // - `write` the defined value for the late resource T - #mangled_name.get_mut_unchecked().as_mut_ptr().write(late.#name); - )); - } + // Initialize shared resources + for (name, res) in &app.shared_resources { + let mangled_name = util::mark_internal_ident(&util::static_shared_resource_ident(name)); + // If it's live + let cfgs = res.cfgs.clone(); + if analysis.shared_resource_locations.get(name).is_some() { + stmts.push(quote!( + // We include the cfgs + #(#cfgs)* + // Resource is a RacyCell> + // - `get_mut_unchecked` to obtain `MaybeUninit` + // - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit` + // - `write` the defined value for the late resource T + #mangled_name.get_mut_unchecked().as_mut_ptr().write(shared_resources.#name); + )); + } + } + + // Initialize local resources + for (name, res) in &app.local_resources { + let mangled_name = util::mark_internal_ident(&util::static_local_resource_ident(name)); + // If it's live + let cfgs = res.cfgs.clone(); + if analysis.local_resource_locations.get(name).is_some() { + stmts.push(quote!( + // We include the cfgs + #(#cfgs)* + // Resource is a RacyCell> + // - `get_mut_unchecked` to obtain `MaybeUninit` + // - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit` + // - `write` the defined value for the late resource T + #mangled_name.get_mut_unchecked().as_mut_ptr().write(local_resources.#name); + )); } } diff --git a/macros/src/codegen/pre_init.rs b/macros/src/codegen/pre_init.rs index 531debac..ae628f6e 100644 --- a/macros/src/codegen/pre_init.rs +++ b/macros/src/codegen/pre_init.rs @@ -126,7 +126,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec), - quote!(rtic::RacyCell::new(#expr)), - ) - } else { - // late resource - ( - quote!(rtic::RacyCell>), - quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit())), - ) - }; + let mangled_name = util::mark_internal_ident(&util::static_shared_resource_ident(&name)); - let attrs = &res.attrs; + // late resources in `util::link_section_uninit` + let section = util::link_section_uninit(true); + let attrs = &res.attrs; - // For future use - // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); - mod_app.push(quote!( - #[allow(non_upper_case_globals)] - // #[doc = #doc] - #[doc(hidden)] - #(#attrs)* - #(#cfgs)* - #section - static #mangled_name: #ty = #expr; - )); - } + // For future use + // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); + mod_app.push(quote!( + #[allow(non_upper_case_globals)] + // #[doc = #doc] + #[doc(hidden)] + #(#attrs)* + #(#cfgs)* + #section + static #mangled_name: rtic::RacyCell> = rtic::RacyCell::new(core::mem::MaybeUninit::uninit()); + )); - let r_prop = &res.properties; // For future use // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); - if !r_prop.task_local && !r_prop.lock_free { + if !res.properties.lock_free { mod_resources.push(quote!( // #[doc = #doc] #[doc(hidden)] @@ -89,25 +66,10 @@ pub fn codegen( } )); - let (ptr, _doc) = if expr.is_none() { - // late resource - ( - quote!( - #(#cfgs)* - #mangled_name.get_mut_unchecked().as_mut_ptr() - ), - "late", - ) - } else { - // early resource - ( - quote!( - #(#cfgs)* - #mangled_name.get_mut_unchecked() - ), - "early", - ) - }; + let ptr = quote!( + #(#cfgs)* + #mangled_name.get_mut_unchecked().as_mut_ptr() + ); let ceiling = match analysis.ownerships.get(name) { Some(Ownership::Owned { priority }) => *priority, @@ -123,7 +85,7 @@ pub fn codegen( extra, cfgs, true, - name, + &name, quote!(#ty), ceiling, ptr, @@ -134,7 +96,7 @@ pub fn codegen( let mod_resources = if mod_resources.is_empty() { quote!() } else { - quote!(mod resources { + quote!(mod shared_resources { use rtic::export::Priority; #(#mod_resources)* diff --git a/macros/src/codegen/shared_resources_struct.rs b/macros/src/codegen/shared_resources_struct.rs index 4bdc8fa1..df7f38de 100644 --- a/macros/src/codegen/shared_resources_struct.rs +++ b/macros/src/codegen/shared_resources_struct.rs @@ -10,7 +10,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, let resources = match ctxt { Context::Init => unreachable!("Tried to generate shared resources struct for init"), - Context::Idle => &app.idle.unwrap().args.shared_resources, + Context::Idle => &app.idle.as_ref().unwrap().args.shared_resources, Context::HardwareTask(name) => &app.hardware_tasks[name].args.shared_resources, Context::SoftwareTask(name) => &app.software_tasks[name].args.shared_resources, }; @@ -48,12 +48,12 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, fields.push(quote!( #(#cfgs)* - pub #name: resources::#name<'a> + pub #name: shared_resources::#name<'a> )); values.push(quote!( #(#cfgs)* - #name: resources::#name::new(priority) + #name: shared_resources::#name::new(priority) )); diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 0372e8ec..da594e7d 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context}; use crate::{ analyze::Analysis, check::Extra, - codegen::{locals, module, resources_struct, util}, + codegen::{local_resources_struct, module, shared_resources_struct, util}, }; pub fn codegen( @@ -91,35 +91,38 @@ pub fn codegen( // `${task}Resources` let mut needs_lt = false; - if !task.args.resources.is_empty() { + + // TODO: Fix locals + // `${task}Locals` + if !task.args.local_resources.is_empty() { let (item, constructor) = - resources_struct::codegen(Context::SoftwareTask(name), &mut needs_lt, app); + local_resources_struct::codegen(Context::SoftwareTask(name), &mut needs_lt, app); root.push(item); mod_app.push(constructor); } - // `${task}Locals` - let mut locals_pat = None; - if !task.locals.is_empty() { - let (struct_, pat) = locals::codegen(Context::SoftwareTask(name), &task.locals, app); + if !task.args.shared_resources.is_empty() { + let (item, constructor) = + shared_resources_struct::codegen(Context::SoftwareTask(name), &mut needs_lt, app); - locals_pat = Some(pat); - root.push(struct_); + root.push(item); + + mod_app.push(constructor); } + if !&task.is_extern { let context = &task.context; let attrs = &task.attrs; let cfgs = &task.cfgs; let stmts = &task.stmts; - let locals_pat = locals_pat.iter(); user_tasks.push(quote!( #(#attrs)* #(#cfgs)* #[allow(non_snake_case)] - fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) { + fn #name(#context: #name::Context #(,#inputs)*) { use rtic::Mutex as _; use rtic::mutex_prelude::*; diff --git a/macros/src/codegen/util.rs b/macros/src/codegen/util.rs index 21b31411..dd0fb6d9 100644 --- a/macros/src/codegen/util.rs +++ b/macros/src/codegen/util.rs @@ -41,7 +41,7 @@ pub fn impl_mutex( ptr: TokenStream2, ) -> TokenStream2 { let (path, priority) = if resources_prefix { - (quote!(resources::#name), quote!(self.priority())) + (quote!(shared_resources::#name), quote!(self.priority())) } else { (quote!(#name), quote!(self.priority)) }; @@ -167,7 +167,7 @@ pub fn link_section_uninit(empty_expr: bool) -> Option { pub fn locals_ident(ctxt: Context, app: &App) -> Ident { let mut s = match ctxt { Context::Init => app.init.name.to_string(), - Context::Idle => app.idle.unwrap().name.to_string(), + Context::Idle => app.idle.as_ref().unwrap().name.to_string(), Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(), }; @@ -229,7 +229,7 @@ pub fn regroup_inputs( pub fn shared_resources_ident(ctxt: Context, app: &App) -> Ident { let mut s = match ctxt { Context::Init => app.init.name.to_string(), - Context::Idle => app.idle.unwrap().name.to_string(), + Context::Idle => app.idle.as_ref().unwrap().name.to_string(), Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(), }; @@ -242,7 +242,7 @@ pub fn shared_resources_ident(ctxt: Context, app: &App) -> Ident { pub fn local_resources_ident(ctxt: Context, app: &App) -> Ident { let mut s = match ctxt { Context::Init => app.init.name.to_string(), - Context::Idle => app.idle.unwrap().name.to_string(), + Context::Idle => app.idle.as_ref().unwrap().name.to_string(), Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(), }; @@ -288,6 +288,14 @@ pub fn monotonic_ident(name: &str) -> Ident { Ident::new(&format!("MONOTONIC_STORAGE_{}", name), Span::call_site()) } +pub fn static_shared_resource_ident(name: &Ident) -> Ident { + Ident::new(&format!("shared_{}", name.to_string()), Span::call_site()) +} + +pub fn static_local_resource_ident(name: &Ident) -> Ident { + Ident::new(&format!("local_{}", name.to_string()), Span::call_site()) +} + /// The name to get better RT flag errors pub fn rt_err_ident() -> Ident { Ident::new( -- cgit v1.2.3 From 8f3704378295fe8007290dbddbc1f4946ac599f9 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Thu, 8 Jul 2021 23:18:44 +0200 Subject: Cleanup from review (needs releases to compile) --- .github/workflows/build.yml | 2 +- Cargo.toml | 11 ++--- examples/t-init-main.rs | 24 ---------- examples/t-resource.rs | 85 ---------------------------------- examples/t-stask-main.rs | 29 ------------ examples/task_named_main.rs | 32 ------------- examples/type-usage.rs | 26 ----------- macros/Cargo.toml | 2 +- macros/src/codegen/local_resources.rs | 2 +- macros/src/codegen/module.rs | 3 -- macros/src/codegen/shared_resources.rs | 2 +- macros/src/codegen/software_tasks.rs | 2 +- macros/src/codegen/util.rs | 9 +--- 13 files changed, 11 insertions(+), 218 deletions(-) delete mode 100644 examples/t-init-main.rs delete mode 100644 examples/t-resource.rs delete mode 100644 examples/t-stask-main.rs delete mode 100644 examples/task_named_main.rs delete mode 100644 examples/type-usage.rs (limited to 'macros/src/codegen/shared_resources.rs') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5f9ad1ff..4d7ed950 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -138,7 +138,7 @@ jobs: with: use-cross: false command: check - args: --examples --target=${{ matrix.target }} --features __min_r1_43,${{ env.V7 }} + args: --examples --target=${{ matrix.target }} --features ${{ env.V7 }} # Verify the example output with run-pass tests testexamples: diff --git a/Cargo.toml b/Cargo.toml index a8903307..cfeb6208 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,10 +31,6 @@ required-features = ["__v7"] name = "schedule" required-features = ["__v7"] -[[example]] -name = "t-cfg-resources" -required-features = ["__min_r1_43"] - [[example]] name = "t-schedule" required-features = ["__v7"] @@ -46,14 +42,16 @@ required-features = ["__v7"] [dependencies] cortex-m = "0.7.0" cortex-m-rtic-macros = { path = "macros", version = "0.6.0-alpha.4" } -rtic-monotonic = "0.1.0-alpha.1" +# rtic-monotonic = "0.1.0-alpha.2" +rtic-monotonic = { path = "../rtic-monotonic" } rtic-core = "0.3.1" heapless = "0.6.1" bare-metal = "1.0.0" generic-array = "0.14" [dependencies.dwt-systick-monotonic] -version = "0.1.0-alpha.2" +# version = "0.1.0-alpha.3" +path = "../dwt-systick-monotonic" optional = true [build-dependencies] @@ -73,7 +71,6 @@ trybuild = "1" [features] # used for testing this crate; do not use in applications __v7 = ["dwt-systick-monotonic"] -__min_r1_43 = [] [profile.release] codegen-units = 1 diff --git a/examples/t-init-main.rs b/examples/t-init-main.rs deleted file mode 100644 index 92717887..00000000 --- a/examples/t-init-main.rs +++ /dev/null @@ -1,24 +0,0 @@ -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -use panic_semihosting as _; - -#[rtic::app(device = lm3s6965)] -mod app { - use cortex_m_semihosting::debug; - - #[shared] - struct Shared {} - - #[local] - struct Local {} - - #[init] - fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { - debug::exit(debug::EXIT_SUCCESS); - - (Shared {}, Local {}, init::Monotonics()) - } -} diff --git a/examples/t-resource.rs b/examples/t-resource.rs deleted file mode 100644 index 27324910..00000000 --- a/examples/t-resource.rs +++ /dev/null @@ -1,85 +0,0 @@ -//! [compile-pass] Check code generation of shared resources - -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -use panic_semihosting as _; - -#[rtic::app(device = lm3s6965)] -mod app { - #[shared] - struct Shared { - o2: u32, // idle - o3: u32, // EXTI0 - o4: u32, // idle - o5: u32, // EXTI1 - s1: u32, // idle & uart0 - s2: u32, // uart0 & uart1 - s3: u32, // idle & uart0 - } - - #[local] - struct Local {} - - #[init] - fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { - ( - Shared { - o2: 0, - o3: 0, - o4: 0, - o5: 0, - s1: 0, - s2: 0, - s3: 0, - }, - Local {}, - init::Monotonics(), - ) - } - - #[idle(shared = [o2, &o4, s1, &s3])] - fn idle(mut c: idle::Context) -> ! { - // owned by `idle` == `&'static mut` - let _: shared_resources::o2 = c.shared.o2; - - // owned by `idle` == `&'static` if read-only - let _: &u32 = c.shared.o4; - - // shared with `idle` == `Mutex` - c.shared.s1.lock(|_| {}); - - // `&` if read-only - let _: &u32 = c.shared.s3; - - loop { - cortex_m::asm::nop(); - } - } - - #[task(binds = UART0, shared = [o3, s1, s2, &s3])] - fn uart0(c: uart0::Context) { - // owned by interrupt == `&mut` - let _: shared_resources::o3 = c.shared.o3; - - // no `Mutex` proxy when access from highest priority task - let _: shared_resources::s1 = c.shared.s1; - - // no `Mutex` proxy when co-owned by cooperative (same priority) tasks - let _: shared_resources::s2 = c.shared.s2; - - // `&` if read-only - let _: &u32 = c.shared.s3; - } - - #[task(binds = UART1, shared = [s2, &o5])] - fn uart1(c: uart1::Context) { - // owned by interrupt == `&` if read-only - let _: &u32 = c.shared.o5; - - // no `Mutex` proxy when co-owned by cooperative (same priority) tasks - let _: shared_resources::s2 = c.shared.s2; - } -} diff --git a/examples/t-stask-main.rs b/examples/t-stask-main.rs deleted file mode 100644 index ee5959de..00000000 --- a/examples/t-stask-main.rs +++ /dev/null @@ -1,29 +0,0 @@ -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -use panic_semihosting as _; - -#[rtic::app(device = lm3s6965, dispatchers = [SSI0])] -mod app { - use cortex_m_semihosting::debug; - - #[shared] - struct Shared {} - - #[local] - struct Local {} - - #[init] - fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { - taskmain::spawn().ok(); - - (Shared {}, Local {}, init::Monotonics()) - } - - #[task] - fn taskmain(_: taskmain::Context) { - debug::exit(debug::EXIT_SUCCESS); - } -} diff --git a/examples/task_named_main.rs b/examples/task_named_main.rs deleted file mode 100644 index b030b5e7..00000000 --- a/examples/task_named_main.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! examples/task_named_main.rs - -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -use panic_semihosting as _; - -#[rtic::app(device = lm3s6965, dispatchers = [SSI0])] -mod app { - use cortex_m_semihosting::{debug, hprintln}; - - #[shared] - struct Shared {} - - #[local] - struct Local {} - - #[init] - fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { - main::spawn().unwrap(); - - (Shared {}, Local {}, init::Monotonics()) - } - - #[task] - fn main(_: main::Context) { - hprintln!("This task is named main, useful for rust-analyzer").unwrap(); - debug::exit(debug::EXIT_SUCCESS); - } -} diff --git a/examples/type-usage.rs b/examples/type-usage.rs deleted file mode 100644 index e5a088f0..00000000 --- a/examples/type-usage.rs +++ /dev/null @@ -1,26 +0,0 @@ -//! examples/type-usage.rs - -#![no_main] -#![no_std] - -use panic_semihosting as _; // panic handler -use rtic::app; - -#[app(device = lm3s6965, dispatchers = [SSI0])] -mod app { - type Test = u32; - - #[shared] - struct Shared {} - - #[local] - struct Local {} - - #[init] - fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { - (Shared {}, Local {}, init::Monotonics {}) - } - - #[task] - fn t1(_: t1::Context, _val: Test) {} -} diff --git a/macros/Cargo.toml b/macros/Cargo.toml index d1038658..3ae66cd2 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -23,4 +23,4 @@ proc-macro-error = "1" quote = "1" syn = "1" # rtic-syntax = "0.5.0-alpha.3" -rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax.git", branch = "resources_take_2" } +rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax.git" } diff --git a/macros/src/codegen/local_resources.rs b/macros/src/codegen/local_resources.rs index c5cddfb3..a9ffa925 100644 --- a/macros/src/codegen/local_resources.rs +++ b/macros/src/codegen/local_resources.rs @@ -28,7 +28,7 @@ pub fn codegen( let attrs = &res.attrs; // late resources in `util::link_section_uninit` - let section = util::link_section_uninit(true); + let section = util::link_section_uninit(); // For future use // let doc = format!(" RTIC internal: {}:{}", file!(), line!()); diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs index 4fba2f38..a59d6628 100644 --- a/macros/src/codegen/module.rs +++ b/macros/src/codegen/module.rs @@ -198,9 +198,6 @@ pub fn codegen( pub use super::#internal_context_name as Context; )); - // not sure if this is the right way, maybe its backwards, - // that spawn_module should put in in root - if let Context::SoftwareTask(..) = ctxt { let spawnee = &app.software_tasks[name]; let priority = spawnee.args.priority; diff --git a/macros/src/codegen/shared_resources.rs b/macros/src/codegen/shared_resources.rs index d6336b32..181832fd 100644 --- a/macros/src/codegen/shared_resources.rs +++ b/macros/src/codegen/shared_resources.rs @@ -24,7 +24,7 @@ pub fn codegen( let mangled_name = util::mark_internal_ident(&util::static_shared_resource_ident(&name)); // late resources in `util::link_section_uninit` - let section = util::link_section_uninit(true); + let section = util::link_section_uninit(); let attrs = &res.attrs; // For future use diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 6941d9a0..cfd21e40 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -45,7 +45,7 @@ pub fn codegen( quote!(rtic::export::Queue(unsafe { rtic::export::iQueue::u8_sc() })), - Box::new(|| util::link_section_uninit(true)), + Box::new(|| util::link_section_uninit()), ) }; mod_app.push(quote!( diff --git a/macros/src/codegen/util.rs b/macros/src/codegen/util.rs index 3b0b9e4f..86bd6955 100644 --- a/macros/src/codegen/util.rs +++ b/macros/src/codegen/util.rs @@ -152,13 +152,8 @@ fn link_section_index() -> usize { } // NOTE `None` means in shared memory -pub fn link_section_uninit(empty_expr: bool) -> Option { - let section = if empty_expr { - let index = link_section_index(); - format!(".uninit.rtic{}", index) - } else { - format!(".uninit.rtic{}", link_section_index()) - }; +pub fn link_section_uninit() -> Option { + let section = format!(".uninit.rtic{}", link_section_index()); Some(quote!(#[link_section = #section])) } -- cgit v1.2.3