diff --git a/macros/Cargo.toml b/macros/Cargo.toml index aa96d09ec6..bfb95250a1 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -22,4 +22,5 @@ proc-macro2 = "1" proc-macro-error = "1" quote = "1" syn = "1" -rtic-syntax = "0.5.0-alpha.3" +# rtic-syntax = "0.5.0-alpha.3" +rtic-syntax = { path = "../../rtic-syntax" } diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 113d17f9f6..f6fdf022a8 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -9,12 +9,13 @@ mod dispatchers; mod hardware_tasks; mod idle; mod init; -mod locals; +mod local_resources; +mod shared_resources; +mod local_resources_struct; +mod shared_resources_struct; mod module; mod post_init; mod pre_init; -mod resources; -mod resources_struct; mod software_tasks; mod timer_queue; mod util; diff --git a/macros/src/codegen/dispatchers.rs b/macros/src/codegen/dispatchers.rs index 65a3f5f649..dfa52d5dc5 100644 --- a/macros/src/codegen/dispatchers.rs +++ b/macros/src/codegen/dispatchers.rs @@ -76,7 +76,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec` -/// Late resource are stored in `RacyCell>` -/// -/// Safety: -/// - RacyCell access is `unsafe`. -/// - RacyCell is always written to before user access, thus -// the generated code for user access can safely `assume_init`. +/// Generates `local` variables and local resource proxies pub fn codegen( app: &App, analysis: &Analysis, @@ -25,7 +18,8 @@ pub fn codegen( let mut mod_app = vec![]; let mut mod_resources = vec![]; - for (name, res, expr, _) in app.resources(analysis) { + 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); diff --git a/macros/src/codegen/local_resources_struct.rs b/macros/src/codegen/local_resources_struct.rs new file mode 100644 index 0000000000..3016f37997 --- /dev/null +++ b/macros/src/codegen/local_resources_struct.rs @@ -0,0 +1,88 @@ +use proc_macro2::TokenStream as TokenStream2; +use quote::quote; +use rtic_syntax::{ast::App, Context}; + +use crate::codegen::util; + +/// Generates local resources structs +pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, TokenStream2) { + let mut lt = None; + + let resources = match ctxt { + Context::Init => &app.init.args.local_resources, + Context::Idle => &app.idle.unwrap().args.local_resources, + Context::HardwareTask(name) => &app.hardware_tasks[name].args.local_resources, + Context::SoftwareTask(name) => &app.software_tasks[name].args.local_resources, + }; + + let mut fields = vec![]; + let mut values = vec![]; + let mut has_cfgs = false; + + for (name, task_local) in resources { + let res = app.local_resources.get(name).expect("UNREACHABLE"); + + let cfgs = &res.cfgs; + has_cfgs |= !cfgs.is_empty(); + + let lt = if ctxt.runs_once() { + quote!('static) + } else { + lt = Some(quote!('a)); + quote!('a) + }; + + let ty = &res.ty; + let mangled_name = util::mark_internal_ident(&name); + + fields.push(quote!( + #(#cfgs)* + pub #name: &#lt mut #ty + )); + + let expr = quote!(&mut *#mangled_name.get_mut_unchecked().as_mut_ptr()); + + values.push(quote!( + #(#cfgs)* + #name: #expr + )); + } + + if lt.is_some() { + *needs_lt = true; + + // The struct could end up empty due to `cfg`s leading to an error due to `'a` being unused + if has_cfgs { + fields.push(quote!( + #[doc(hidden)] + pub __marker__: core::marker::PhantomData<&'a ()> + )); + + values.push(quote!(__marker__: core::marker::PhantomData)) + } + } + + let doc = format!("Local resources `{}` has access to", ctxt.ident(app)); + let ident = util::local_resources_ident(ctxt, app); + let ident = util::mark_internal_ident(&ident); + let item = quote!( + #[allow(non_snake_case)] + #[doc = #doc] + pub struct #ident<#lt> { + #(#fields,)* + } + ); + + let constructor = quote!( + impl<#lt> #ident<#lt> { + #[inline(always)] + pub unsafe fn new() -> Self { + #ident { + #(#values,)* + } + } + } + ); + + (item, constructor) +} diff --git a/macros/src/codegen/locals.rs b/macros/src/codegen/locals.rs deleted file mode 100644 index 0fb8c6d298..0000000000 --- a/macros/src/codegen/locals.rs +++ /dev/null @@ -1,95 +0,0 @@ -use proc_macro2::TokenStream as TokenStream2; -use quote::quote; -use rtic_syntax::{ - ast::{App, Local}, - Context, Map, -}; - -use crate::codegen::util; - -pub fn codegen( - ctxt: Context, - locals: &Map, - app: &App, -) -> ( - // locals - TokenStream2, - // pat - TokenStream2, -) { - assert!(!locals.is_empty()); - - let runs_once = ctxt.runs_once(); - let ident = util::locals_ident(ctxt, app); - - let mut lt = None; - let mut fields = vec![]; - let mut items = vec![]; - let mut names = vec![]; - let mut values = vec![]; - let mut pats = vec![]; - let mut has_cfgs = false; - - for (name, local) in locals { - let lt = if runs_once { - quote!('static) - } else { - lt = Some(quote!('a)); - quote!('a) - }; - - let cfgs = &local.cfgs; - has_cfgs |= !cfgs.is_empty(); - - let expr = &local.expr; - let ty = &local.ty; - fields.push(quote!( - #(#cfgs)* - #name: &#lt mut #ty - )); - items.push(quote!( - #(#cfgs)* - #[doc(hidden)] - static #name: rtic::RacyCell<#ty> = rtic::RacyCell::new(#expr) - )); - values.push(quote!( - #(#cfgs)* - #name: #name.get_mut_unchecked() - )); - names.push(name); - pats.push(quote!( - #(#cfgs)* - #name - )); - } - - if lt.is_some() && has_cfgs { - fields.push(quote!(__marker__: core::marker::PhantomData<&'a ()>)); - values.push(quote!(__marker__: core::marker::PhantomData)); - } - - let locals = quote!( - #[allow(non_snake_case)] - #[doc(hidden)] - pub struct #ident<#lt> { - #(#fields),* - } - - impl<#lt> #ident<#lt> { - #[inline(always)] - unsafe fn new() -> Self { - #(#items;)* - - #ident { - #(#values),* - } - } - } - ); - - let ident = ctxt.ident(app); - ( - locals, - quote!(#ident::Locals { #(#pats,)* .. }: #ident::Locals), - ) -} diff --git a/macros/src/codegen/shared_resources.rs b/macros/src/codegen/shared_resources.rs new file mode 100644 index 0000000000..f6d2dcb008 --- /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) +} diff --git a/macros/src/codegen/resources_struct.rs b/macros/src/codegen/shared_resources_struct.rs similarity index 73% rename from macros/src/codegen/resources_struct.rs rename to macros/src/codegen/shared_resources_struct.rs index 6fe4678a62..4bdc8fa128 100644 --- a/macros/src/codegen/resources_struct.rs +++ b/macros/src/codegen/shared_resources_struct.rs @@ -4,14 +4,15 @@ use rtic_syntax::{ast::App, Context}; use crate::codegen::util; +/// Generate shared resources structs pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, TokenStream2) { let mut lt = None; let resources = match ctxt { - Context::Init => &app.inits.first().unwrap().args.resources, - Context::Idle => &app.idles.first().unwrap().args.resources, - Context::HardwareTask(name) => &app.hardware_tasks[name].args.resources, - Context::SoftwareTask(name) => &app.software_tasks[name].args.resources, + Context::Init => unreachable!("Tried to generate shared resources struct for init"), + Context::Idle => &app.idle.unwrap().args.shared_resources, + Context::HardwareTask(name) => &app.hardware_tasks[name].args.shared_resources, + Context::SoftwareTask(name) => &app.software_tasks[name].args.shared_resources, }; let mut fields = vec![]; @@ -19,7 +20,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, let mut has_cfgs = false; for (name, access) in resources { - let (res, expr) = app.resource(name).expect("UNREACHABLE"); + let res = app.shared_resources.get(name).expect("UNREACHABLE"); let cfgs = &res.cfgs; has_cfgs |= !cfgs.is_empty(); @@ -33,10 +34,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 ownership = &analysis.ownerships[name]; - let r_prop = &res.properties; - - if !r_prop.task_local && !r_prop.lock_free { + if !res.properties.lock_free { if access.is_shared() { lt = Some(quote!('a)); @@ -76,24 +74,16 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, )); } - let is_late = expr.is_none(); - if is_late { - let expr = if access.is_exclusive() { - quote!(&mut *#mangled_name.get_mut_unchecked().as_mut_ptr()) - } else { - quote!(&*#mangled_name.get_unchecked().as_ptr()) - }; - - values.push(quote!( - #(#cfgs)* - #name: #expr - )); + let expr = if access.is_exclusive() { + quote!(&mut *#mangled_name.get_mut_unchecked().as_mut_ptr()) } else { - values.push(quote!( - #(#cfgs)* - #name: #mangled_name.get_mut_unchecked() - )); - } + quote!(&*#mangled_name.get_unchecked().as_ptr()) + }; + + values.push(quote!( + #(#cfgs)* + #name: #expr + )); } if lt.is_some() { @@ -110,8 +100,8 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, } } - let doc = format!("Resources `{}` has access to", ctxt.ident(app)); - let ident = util::resources_ident(ctxt, app); + let doc = format!("Shared resources `{}` has access to", ctxt.ident(app)); + let ident = util::shared_resources_ident(ctxt, app); let ident = util::mark_internal_ident(&ident); let item = quote!( #[allow(non_snake_case)] diff --git a/macros/src/codegen/util.rs b/macros/src/codegen/util.rs index 3e42eda96b..21b314116e 100644 --- a/macros/src/codegen/util.rs +++ b/macros/src/codegen/util.rs @@ -166,8 +166,8 @@ pub fn link_section_uninit(empty_expr: bool) -> Option { /// Generates a pre-reexport identifier for the "locals" struct pub fn locals_ident(ctxt: Context, app: &App) -> Ident { let mut s = match ctxt { - Context::Init => app.inits.first().unwrap().name.to_string(), - Context::Idle => app.idles.first().unwrap().name.to_string(), + Context::Init => app.init.name.to_string(), + Context::Idle => app.idle.unwrap().name.to_string(), Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(), }; @@ -225,15 +225,28 @@ pub fn regroup_inputs( } } -/// Generates a pre-reexport identifier for the "resources" struct -pub fn resources_ident(ctxt: Context, app: &App) -> Ident { +/// Generates a pre-reexport identifier for the "shared resources" struct +pub fn shared_resources_ident(ctxt: Context, app: &App) -> Ident { let mut s = match ctxt { - Context::Init => app.inits.first().unwrap().name.to_string(), - Context::Idle => app.idles.first().unwrap().name.to_string(), + Context::Init => app.init.name.to_string(), + Context::Idle => app.idle.unwrap().name.to_string(), Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(), }; - s.push_str("Resources"); + s.push_str("SharedResources"); + + Ident::new(&s, Span::call_site()) +} + +/// Generates a pre-reexport identifier for the "local resources" struct +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::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(), + }; + + s.push_str("LocalResources"); Ident::new(&s, Span::call_site()) }